Jeff Ward
2006-May-10 17:30 UTC
[Rails] Action Variables Not Available In The View For A Specific Action
I am setting an action variable in my PagesController such as: def manage render(:layout => "admin") @strOut = "STRING Variable Set From The MANAGE Action!" end And in the /views/pages/manage.rhtml view, I don?t have access to that @strOut variable by trying to call it as: <%= @strOut %> However, I am setting the same variable in another action in the controller such as: def view @strOut = "STRING Variable Set From The EDIT Action!" end ... and I have full access to it in /views/pages/view.rhtml The only difference in the actions being this: render(:layout => "admin") ...in the manage action. So, I want the manage action to use /views/layouts/admin.rhtml, and the edit action to use /views/layouts/pages.rhtml. I''m not sure if the layouts have anything to do with it but I really don''t know why that variable cannot be called from the view. -- ? Jeff ?
Matt Bauer
2006-May-10 18:10 UTC
[Rails] Re: Action Variables Not Available In The View For A Specifi
It''s an order thing. render :layout => ''admin'' actually copies the instance variables from the controller for use in the template. It''s actually a bit more complicated than that but that''s the just. In otherwards, at the time the instance variables are copied to the template, your instance variable @strOut does not exist and is not copied. Just code your method like: def manage @strOut = "BLAH" render :action => ''admin'' end and all should be good. -Matt -- Posted via http://www.ruby-forum.com/.
Jeff Ward
2006-May-10 18:13 UTC
[Rails] Re: Action Variables Not Available In The View For A Specifi
Excellent, re-ordering them worked. Thanks very much. -- Jeff -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Matt Bauer Sent: May 10, 2006 11:10 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: Action Variables Not Available In The View For A Specifi It''s an order thing. render :layout => ''admin'' actually copies the instance variables from the controller for use in the template. It''s actually a bit more complicated than that but that''s the just. In otherwards, at the time the instance variables are copied to the template, your instance variable @strOut does not exist and is not copied. Just code your method like: def manage @strOut = "BLAH" render :action => ''admin'' end and all should be good. -Matt -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jeremy Kemper
2006-May-10 18:44 UTC
[Rails] Action Variables Not Available In The View For A Specific Action
On May 10, 2006, at 10:31 AM, Jeff Ward wrote:> I am setting an action variable in my PagesController such as: > > def manage > render(:layout => "admin") > @strOut = "STRING Variable Set From The MANAGE Action!" > end > > And in the /views/pages/manage.rhtml view, I don?t have access to that > @strOut variable by trying to call it as: <%= @strOut %>You set the variable after the view was rendered. Swap those two lines. jeremy
Maybe Matching Threads
- Using set_primary_key breaks acts_as_tree with non-integer column
- warning: toplevel constant XYZ referenced Admin:XYZ
- rename multiple files by file.rename or other functions
- RSpec, RESTful nested routes and :path_prefix
- Dashboard throwing undefined method `to_time' for nil:NilClass error on page view