David Kahn
2010-Oct-28 21:10 UTC
Rails 3 - possible to render a view into a string (inside a model or controller)?
In Rails 2 I could do this to get the view as a string (i.e. to save it to a file): av = ActionView::Base.new(Rails::Configuration.new.view_path) html = av.render( :partial => "comparisons/display", :locals => { :comparison => self, :format_html => nil, :print_view => print_view, :fully_qualified_url_prefix => fully_qualified_url_prefix, :user => user } ) In Rails 3 it seemingly does not work anymore. In reference to https://rails.lighthouseapp.com/projects/8994/tickets/3471-actionviewassigns-lost-its-usefulness, Yehuda Katz''s comment (in reference to passing necessary parameters to ActionView::Base.new) is "This was modified in order to improve performance and reduce the number of instance variable assigns we need to do in cases with many renders inside a single view." So... is there a way to do this anyone knows of in Rails 3? Even if I can do so inside a controller I guess that would also be ok (I will try this next), but would prefer to leave things as they are. Thanks, David -- 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.
Marnen Laibow-Koser
2010-Oct-28 21:30 UTC
Re: Rails 3 - possible to render a view into a string (inside a model or controller)?
David Kahn wrote in post #957895:> In Rails 2 I could do this to get the view as a string (i.e. to save it > to a > file): > > av = ActionView::Base.new(Rails::Configuration.new.view_path) > html = av.render( > :partial => "comparisons/display", > :locals => { :comparison => self, :format_html => nil, > :print_view > => print_view, :fully_qualified_url_prefix => > fully_qualified_url_prefix, > :user => user } > ) > > In Rails 3 it seemingly does not work anymore.Did you perchance want render_to_string (which also exists in Rails 2)? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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.
David Kahn
2010-Oct-28 21:38 UTC
Re: Re: Rails 3 - possible to render a view into a string (inside a model or controller)?
On Thu, Oct 28, 2010 at 4:30 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> David Kahn wrote in post #957895: > > In Rails 2 I could do this to get the view as a string (i.e. to save it > > to a > > file): > > > > av = ActionView::Base.new(Rails::Configuration.new.view_path) > > html = av.render( > > :partial => "comparisons/display", > > :locals => { :comparison => self, :format_html => nil, > > :print_view > > => print_view, :fully_qualified_url_prefix => > > fully_qualified_url_prefix, > > :user => user } > > ) > > > > In Rails 3 it seemingly does not work anymore. > > Did you perchance want render_to_string (which also exists in Rails 2)? >Hmmm... yup, that works, thanks. Would rather do that in the model but I can pass it in and step aside from this mess. I must have been hankering for punishment in doing it the way I did it in the first place.> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.
David Kahn
2010-Oct-28 21:46 UTC
Re: Re: Rails 3 - possible to render a view into a string (inside a model or controller)?
On Thu, Oct 28, 2010 at 4:38 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> > > On Thu, Oct 28, 2010 at 4:30 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org > > wrote: > >> David Kahn wrote in post #957895: >> > In Rails 2 I could do this to get the view as a string (i.e. to save it >> > to a >> > file): >> > >> > av = ActionView::Base.new(Rails::Configuration.new.view_path) >> > html = av.render( >> > :partial => "comparisons/display", >> > :locals => { :comparison => self, :format_html => nil, >> > :print_view >> > => print_view, :fully_qualified_url_prefix => >> > fully_qualified_url_prefix, >> > :user => user } >> > ) >> > >> > In Rails 3 it seemingly does not work anymore. >> >> Did you perchance want render_to_string (which also exists in Rails 2)? >> > > Hmmm... yup, that works, thanks. Would rather do that in the model but I > can pass it in and step aside from this mess. I must have been hankering for > punishment in doing it the way I did it in the first place. >Actually just discovered the reason I tried to do this in the model: I need to render this view to string from other controllers/models. Do you know offhand if I can just instantiate this controller and still call one of it''s methods to string from a different controller or even better from a model? Just trying it right now and getting "NameError Exception: uninitialized constant InstallerController::ComparisonController" (foreign controller name is InstallerController).>> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- 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.
Marnen Laibow-Koser
2010-Oct-28 21:53 UTC
Re: Re: Rails 3 - possible to render a view into a string (inside a model or controller)?
David Kahn wrote in post #957906:> On Thu, Oct 28, 2010 at 4:38 PM, David Kahn > <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote: > >>> > av = ActionView::Base.new(Rails::Configuration.new.view_path) >>> >>> Did you perchance want render_to_string (which also exists in Rails 2)? >>> >> >> Hmmm... yup, that works, thanks. Would rather do that in the model but I >> can pass it in and step aside from this mess. I must have been hankering for >> punishment in doing it the way I did it in the first place. >> > > Actually just discovered the reason I tried to do this in the model: I > need > to render this view to string from other controllers/models.You should never be rendering anything from a model. Any rendering -- at all -- is the responsibility of the controller and view.> Do you know > offhand if I can just instantiate this controller and still call one of > it''s > methods to string from a different controller or even better from a > model?render_to_string :controller => ''some_other_controller'', :action => ''export_file'' That should be all you need, just like plain old render. Does that not work? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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.
David Kahn
2010-Oct-28 21:58 UTC
Re: Re: Re: Rails 3 - possible to render a view into a string (inside a model or controller)?
On Thu, Oct 28, 2010 at 4:53 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> David Kahn wrote in post #957906: > > > Do you know > > offhand if I can just instantiate this controller and still call one of > > it''s > > methods to string from a different controller or even better from a > > model? > > render_to_string :controller => ''some_other_controller'', :action => > ''export_file'' > > That should be all you need, just like plain old render. Does that not > work? >Ok, I am loosing my mind, yes, this works beautifully! I did not make the connection between render_to_string and the normal render action being of the same abilities.> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.