Dmitry Suzdalev
2011-Oct-12 20:19 UTC
How to access controller''s instance variable in a js.erb loaded by javascript_include_tag?
Hello! In rails 3.1 app I have a controller UsersController with ''show'' action. show.html.erb contains: <% content_for(:head) do %> <%= javascript_include_tag ''myscript'' %> <% end %> <p>Hello @user.name</p> And I have this in myscript.js.erb $jQuery(document).ready(function() { alert(<%= @user.name %>); }); @user is being assigned by a controller and it works just ok in show.html.erb, but in myscript.js.erb I get nil as @user and exception is thrown. What''s my error here? How to get around this? I googled, but all suggestions I found were about rendering partial and using :local option to pass a variable to it. Is this the only option? I''m asking because I suspect I miss something. It would look really natural for @user being accessible in js.erb loaded through javascript_include_tag... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ocxAmLujxpYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2011-Oct-12 22:15 UTC
Re: How to access controller''s instance variable in a js.erb loaded by javascript_include_tag?
You render the value in the template and grab it via javascript, like this: <%= tag :meta, :name=> ''user_name'', :content=> @user.name%> $(''meta[name=user_name]'').attr(''content''); -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dmitry Suzdalev
2011-Oct-13 07:29 UTC
Re: How to access controller''s instance variable in a js.erb loaded by javascript_include_tag?
And what if it is a large array of values?... Ok to pass them as a contents of the tag? I know I could do it with ajax request, but how do I pass this large amount of data when JS is disabled... What I''m doing is feeding data to jQuery Flot library. And it accepts data in place of a plot initialization, e.g. $.plot("#placeholder", data) so I need to somehow inline this data when initializing element => js.erb. And then I expect to have something like: $.plot("#placeholder", <%= @user.generate_flot_data %>) I''m a bit new to this stuff, may ask stupid questions :) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/X-id2vt_TEMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Oct-13 13:24 UTC
Re: How to access controller''s instance variable in a js.erb loaded by javascript_include_tag?
On Oct 12, 9:19 pm, Dmitry Suzdalev <dim...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello! > > In rails 3.1 app I have a controller UsersController with ''show'' action. > show.html.erb contains: > > <% content_for(:head) do %> > <%= javascript_include_tag ''myscript'' %> > <% end %> > <p>Hello @user.name</p> > > And I have this in myscript.js.erb > > $jQuery(document).ready(function() { > alert(<%= @user.name %>); > > }); > > @user is being assigned by a controller and it works just ok in > show.html.erb, but in myscript.js.erb I get nil as @user and exception is > thrown. > > What''s my error here? How to get around this? > I googled, but all suggestions I found were about rendering partial and > using :local option to pass a variable to it. > Is this the only option? > I''m asking because I suspect I miss something. It would look really natural > for @user being accessible in js.erb loaded through > javascript_include_tag...javascript_include_tag generates a <script> tag in the output. When the browser renders your page, it looks at that script tag and requests myscript.js from your server. By now, the original controller and its @user variable are long gone, you''re instead looking at the @user from a new controller, which is apparently null. one way of dealing this might be <% content_for :head do %> <%= javascript_tag "var js_user_name = #{@user.name.to_json};" <%= javascript_include_tag ''my script'' %> <% end %> which creates a (javascript) variable called js_user_name that your javascript would be able to use. Another option would be to pass information about which user to pick via a query parameter (i.e. instead of the url requested being /javascripts/myscript.js it would be /javascripts/myscript.js?user_id=123), and have the controller that renders the javascript set up @user accordingly. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dmitry Suzdalev
2011-Oct-14 09:21 UTC
Re: How to access controller''s instance variable in a js.erb loaded by javascript_include_tag?
Thank you very much, Frederick, very useful! I think I will go on with the first method (for starters)! :-) Cheers, Dmitry. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ncuspDRxtCcJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Maybe Matching Threads
- Rearchitect the new :cache feature of javascript_include_tag, similar to ToolbawksPacker
- Stubbing #javascript_include_tag in a helper spec
- export variable from bash to R
- passing command line arguments to 'R CMD BATCH myScript.R'
- javascript_include_tag error