简体   繁体   中英

(rails3) passing ruby variables to javascript file

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.

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' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM