I want a method in app/controllers/application.rb that can prepend/append text to whatever template gets rendered. Of course I can''t call render twice w/o getting a double render error, so is this possible? I want to redirect after a delay using a meta refresh. Here''s what I''ve got: app/controllers/application_controller.rb: def redirect_after_delay (url, delay) @redirect_delay = delay @redirect_url = url render end app/views/layouts/application.html.erb <!DOCTYPE html> <html lang="en"> <head> <%= yield :refresh_tag %> </head> <body> <%= yield %> </body> </html> So then if I want to add a redirect-after-delay, I add the following to 1) my controller and 2) the action''s view: app/controllers/my_controller.rb def my_action redirect_after_delay ''http://www.google.com'', 3 if some_condition end app/views/my_controller/my_action.html.erb <% content_for :refresh_tag do %> <meta http-equiv=''refresh'' content=''<%=@redirect_delay%>;url=<%=@redirect_url%>''> <% end %> <h1>Please wait while you are redirected...</h1> Since the content_for block never changes, would be nice to just call something from the redirect_after_delay method to add it to whatever template is going to be rendered. -- 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.
Hello-- On Jan 30, 2010, at 7:38 PM, Aaron Stacy wrote:> > I want a method in app/controllers/application.rb that can > prepend/append text to whatever template gets rendered. Of course I > can''t call render twice w/o getting a double render error, so is this > possible? > > I want to redirect after a delay using a meta refresh. Here''s what I''ve > got: > > app/controllers/application_controller.rb: > > def redirect_after_delay (url, delay) > @redirect_delay = delay > @redirect_url = url > render > end > > app/views/layouts/application.html.erb > > <!DOCTYPE html> > <html lang="en"> > <head> > <%= yield :refresh_tag %> > </head> > > <body> > <%= yield %> > </body> > </html> > > So then if I want to add a redirect-after-delay, I add the following to > 1) my controller and 2) the action''s view: > > app/controllers/my_controller.rb > > def my_action > redirect_after_delay ''http://www.google.com'', 3 if some_condition > end > > app/views/my_controller/my_action.html.erb > > <% content_for :refresh_tag do %> > <meta http-equiv=''refresh'' > content=''<%=@redirect_delay%>;url=<%=@redirect_url%>''> > <% end %> > <h1>Please wait while you are redirected...</h1> > > Since the content_for block never changes, would be nice to just call > something from the redirect_after_delay method to add it to whatever > template is going to be rendered.Check out after filters: http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html -- 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-Jan-31 07:08 UTC
Re: Append/Prepend to a template from controller method
Aaron Stacy wrote:> I want a method in app/controllers/application.rb that can > prepend/append text to whatever template gets rendered. >[...] This is probably best done in the application layout, or else in a before_filter. 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.
Marnen Laibow-Koser wrote:> This is probably best done in the application layout, or else in a > before_filter.I think you''re right, I may have been trying to make it too complicated. Thanks for the suggestions. -- 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.