I am trying to pass the data from controller to javascript. This is how
you do it,
respond_to do |format|
format.html
format.json { render json: {data: @data} }
end
And then in your view file, you should do this:
<%= javascript_tag do%>
window.data = <%= raw @data.to_json %>
<%end%>
Make sense. Right?
However, above code is when you are passing only one variable in json
code i.e. data variable. What I need to do is to pass multiple variables
to my javascript code.
It should be done as:
respond_to do |format|
format.html
format.json { render json: {all_data: {data: @data, data1: @data1,
data2: @data2}}}
end
Now, - How should I access the data1 & data2 variables in my view file?
My view file has:
<%= javascript_tag do%>
window.data = <%= raw @all_data.to_json %>
<%end%>
Above alerts me NULL. What have I done wrong? Where have I messed up?
Any leads would be appreciated.
Cheers!