When rendering a javascript file (not RJS, just a regular JS file) from a rails controller, is there a way to pass variables to it, which can then be used by functions in the JS file being called? I've tried :locals => {} but that doesn't work.
A:
If you're rendering inside a js file you should use rjs. If you really don't like rjs (understandable) then how about rendering your variables first, then including your script?
<% javascript_tag do %>
var my_var = <%= @my_var.to_json %>;
<% end %>
<% javascript_include_tag 'your-js-file-that-expects-my_var-to-exist.js' %>
jdeseno
2010-08-12 17:03:56
Thanks. I have been using RJS, was just looking for a way to avoid it. I guess I could also just go straight to the view and call the JS function (in say, application.js) from the view, with whatever vars in needs?
insane.dreamer
2010-08-12 19:40:52