I need to use a view to generate some text, using render_to_string. The form I am using is: render_to_string :template => "foo/bar" The problem is that this form does not accept the :locals attribute. Therefore any variables I want to be accessible in the view must be instance variables. I understand that this is standard view behavior, but whereas the point of most controllers is to generate instance variables for the view, in my case that''s secondary. And this scheme means I have to think through ahead of time what will be displayed in the view and make sure that variable has an @ in front of it. Is there a more concrete way of passing local variables to this form of render/render_to_string? Thanks! /adam -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jan-20 18:53 UTC
Re: How to pass local variables to render_to_string?
Hi -- On Sat, 20 Jan 2007, Adam Block wrote:> > I need to use a view to generate some text, using render_to_string. The > form I am using is: > > render_to_string :template => "foo/bar" > > The problem is that this form does not accept the :locals attribute. > Therefore any variables I want to be accessible in the view must be > instance variables. > > I understand that this is standard view behavior, but whereas the point > of most controllers is to generate instance variables for the view, in > my case that''s secondary. And this scheme means I have to think through > ahead of time what will be displayed in the view and make sure that > variable has an @ in front of it. > > Is there a more concrete way of passing local variables to this form of > render/render_to_string?The first thing that comes to mind is putting the values in a hash, rather than local variables, and passing that hash along in one instance variable. I don''t know how that would play with the rest of what you''re doing though. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David wrote:> The first thing that comes to mind is putting the values in a hash, > rather than local variables, and passing that hash along in one > instance variable.Hi David. Yes, that would be cleaner, or at least more deliberate (since the packaging of the local controller variables into an instance hash would all happen in one place instead of the instance variable assignments floating all over the controller method). Thanks! /afb -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---