Sebastian Probst Eide
2006-Oct-01 17:15 UTC
Strange behaviour with before_filter and instance variables
Dear message-board users I am still learning Ruby on Rails so the answer to the question I am posting might be obvious, but I can''t see any logical reason why I am getting this strange behaviour, so here goes: To test what I had learned in the Ajax chapter of Agile Web development with Rails I created a simple app which presents the user with a page that has three remote links back to the server. Only one of the links has a method in the controller. The other two just invoke one view each. Since I didn''t want the layout to be rendered for any of the three as they were generating output for an ajax-request, and to honour the DRY principle at the same time, I created a before_filter executing a method which set rendering of the layout to false when the three methods were called. But for some reason instance variables aren''t passed to the view when I set the layout to false in the before filter. To get the variables passed along to the view I have to set the rendering of the layout to false in the method itself. Why is that? Does that make any sense? I''ll include the source code to make it easier to understand what works and what doesn''t: ######################### ######################### #### DOES WORK ######## ######################### # in this version the before filter is only set for # the two ajax-calls which don''t have methods # in the controller but just render views before_filter :no_layout, :only => [:link1, :link2] def navn # @resultat is passed to the view @resultat = finn_navn(params[:navn]) render :layout => false end private def no_layout render :layout => false end ######################### #### DOES NOT WORK #### ######################### # in this version the before filter is set for all # three ajax-calls. :navn is the only one that has a # method in the controller before_filter :no_layout, :only => [:link1, :link2, :navn] def navn # @resultat is NOT passed to the view @resultat = finn_navn(params[:navn]) end private def no_layout render :layout => false end ######################### Thank you. Best regards Sebastian Probst Eide --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2006-Oct-06 21:40 UTC
Re: Strange behaviour with before_filter and instance variables
Sebastian Probst Eide wrote:> ######################### > #### DOES NOT WORK #### > ######################### > # in this version the before filter is set for all > # three ajax-calls. :navn is the only one that has a > # method in the controller > before_filter :no_layout, :only => [:link1, :link2, :navn] > > def navn > # @resultat is NOT passed to the view > @resultat = finn_navn(params[:navn]) > end > > private > def no_layout > render :layout => false > endThe view template is rendered at the point render is called. In the above code the render is happening in a before_filter, before the instance variable is set in navn. Instead of the before_filter, write: layout nil, :only => [:link1, :link2, :navn] -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2006-Oct-07 14:34 UTC
Re: Strange behaviour with before_filter and instance variab
Mark Reginald James wrote:> Sebastian Probst Eide wrote: > >> @resultat = finn_navn(params[:navn]) >> end >> >> private >> def no_layout >> render :layout => false >> end > > The view template is rendered at the point render is called. > In the above code the render is happening in a before_filter, > before the instance variable is set in navn. > > Instead of the before_filter, write: > > layout nil, :only => [:link1, :link2, :navn] > > > -- > We develop, watch us RoR, in numbers too big to ignore.Also, remember that calling render, redirect_to, or returning false from a before_filter will halt the filter chain and prevent subsequent filters and the action itself from ever executing. -- 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 -~----------~----~----~----~------~----~------~--~---
Sebastian Probst Eide
2006-Nov-11 08:02 UTC
Re: Strange behaviour with before_filter and instance variables
Aha! That explains it! Thank you very much! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---