# Passing dynamic ruby variables into a .js.erb file

**URL:** <https://discourse.chef.io/t/passing-dynamic-ruby-variables-into-a-js-erb-file/8170>\
**Category:** Chef Infra (archive)\
**Created:** [April 6, 2016, 9:29am UTC](https://discourse.chef.io/t/passing-dynamic-ruby-variables-into-a-js-erb-file/8170 "2016-04-06T09:29:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [April 6, 2016, 9:29am UTC](https://discourse.chef.io/t/passing-dynamic-ruby-variables-into-a-js-erb-file/8170/1 "2016-04-06T09:29:11Z")

</div>

Hi,

I’m hoping someone could please assist with this problem.

I’ve  
got a scenario where I use these dynamic variables within a template  
.dashboard.js.erb file (using it to construct a grafana dashboard):

#dashboard.js.erb

/\* global \_ \*/

‘use strict’;

var windows, document, $, jQuery, moment, kbn, ARGS;  
var dashboard;  
var rows = 5;  
var env = \<%= metric %\>;  
var nodes\_env = \<%= node %\>;  
…

and my recipe uses the following resource:-

#recipe.rb

dashboard\_path = "/usr/share/public/grafana"  
file = "dashboard.js"  
metric = "blue-monolith"  
node = “nodes.production\_blue”

template File.join(dashboard\_path, file) do  
source "dashboard.js.erb"  
variables({  
:metric =\> metric,  
:node =\> node  
})  
end  
end

I’ve tried the following inside my .js.erb file:-  
var env = \<%= metric %\>;  
var env = ‘\<%= metric %\>’;  
var env = \<%= metric.to\_json %\>;

All of which result in error at the end of a chef run:-  
Chef::Mixin::Template::TemplateError (undefined local variable or method `metric’ for #Chef::Mixin::Template::TemplateContext:0x00000005f49ed0)

Does anyone know how to represent these dynamic variables (metric and node) in a .js.erb file?

Many thanks

---

<div class="post-metadata">

**Author:** ![chris\_sullivan](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chris\_sullivan](https://discourse.chef.io/u/chris_sullivan)\
**Post date:** [April 6, 2016, 10:09am UTC](https://discourse.chef.io/t/passing-dynamic-ruby-variables-into-a-js-erb-file/8170/2 "2016-04-06T10:09:54Z")

</div>

Hi Angela,

You are passing in a Ruby symbol so try

var env = \<%= @metric %\>;

OR

var env = \<%= @:metric %\>;

---

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [April 6, 2016, 11:56am UTC](https://discourse.chef.io/t/passing-dynamic-ruby-variables-into-a-js-erb-file/8170/3 "2016-04-06T11:56:27Z")

</div>

Hi Chris,

Thanks you very much! That worked.
