javascript - in rails I'm sending a var to view and but in js file is nil -


i'm sending var in rails use in view, have js file name charts.js.erb called , need use var in js file nil

in controller have method

  def index     @asd = 'hellow'   end 

in view can do

.div= @asd 

but in javascript file

alert('<%= @asd %>'); 

@asd var nil, i've tested using other ruby code works, vars doesn't work

that's not how erb works javascript. js files generated before runtime, not have access dynamic server values that.

the pattern follow place server-generated value somewhere in dom js pick up.

<div id="asd" data-asd="<%= @asd %>"> 

then if you're using jquery, example:

var asd = $('#asd').data('asd'); 

Comments