If I do something like this: class ApplicationController < ActionController::Base protect_from_forgery before_filter RubyCAS::Filter before_filter :fetch_operator include SessionsHelper private def fetch_operator @current_operator ||= session[:cas_user] && Operator.find_by_uid(session[:cas_user]) rescue ActiveRecord::RecordNotFound redirect_to("/404.html") end end The fetch_operator action is always executed, before every action in every controller? I''d want to fetch the operator only at start and not before every action but I need to have it available on every view and controller. -- 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.
Hi,> The fetch_operator action is always executed, before every action in every controller?Yes, all kind of filters implemented inside ApplicationController class are always executed in every controller (whose parent is ApplicationController) unless you change the behavior explicitly. class ApplicationController < ActionController::Base ... before_filter :fetch_operator ... end class anotherController < ApplicationController before_filter :fetch_operator, :only => [:login] end For further information, see: http://rails.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html Exequiel. -- 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.
On 24 February 2011 08:21, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I do something like this: > > [snip controller code] > > The fetch_operator action is always executed, before every action in > every controller? > I''d want to fetch the operator only at start and not before every > action but I need to have it available on every view and controller.Are you then using the @current_operator variable in places in your controller? (like "if @current_operator.name == ''fred''..."?) If so, instead use a helper method to return the operator (and populate it if necessary), rather than the filter method: class ApplicationController < ActionController::Base protect_from_forgery before_filter RubyCAS::Filter include SessionsHelper helper_method :current_operator def current_operator @current_operator ||= fetch_operator end private def fetch_operator session[:cas_user] && Operator.find_by_uid(session[:cas_user]) rescue ActiveRecord::RecordNotFound redirect_to("/404.html") end end -- 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.
On 24 February 2011 13:37, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24 February 2011 08:21, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> If I do something like this: >> >> [snip controller code] >> >> The fetch_operator action is always executed, before every action in >> every controller? >> I''d want to fetch the operator only at start and not before every >> action but I need to have it available on every view and controller. > > Are you then using the @current_operator variable in places in your > controller? (like "if @current_operator.name == ''fred''..."?) > > If so, instead use a helper method to return the operator (and > populate it if necessary), rather than the filter method: > > > class ApplicationController < ActionController::Base > protect_from_forgery > before_filter RubyCAS::Filter > include SessionsHelper > helper_method :current_operator > > def current_operator > @current_operator ||= fetch_operator > end > > private > def fetch_operator > session[:cas_user] && > Operator.find_by_uid(session[:cas_user]) > rescue ActiveRecord::RecordNotFound > redirect_to("/404.html") > end > > endBut if I want fetch the operator only once at application startup? -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> But if I want fetch the operator only once at application startup?I guess you can set up a session variable to make sure to call only once. if !session[:operator_called] ... session[:operator_called] = true end -- 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.
On 24 February 2011 17:48, exequiel <efulet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>> But if I want fetch the operator only once at application startup? > > I guess you can set up a session variable to make sure to call only > once. > > if !session[:operator_called] > ... > session[:operator_called] = true > end >In a method of application controller? -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Feb 24, 2011 at 4:29 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24 February 2011 17:48, exequiel <efulet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> But if I want fetch the operator only once at application startup? > > > > I guess you can set up a session variable to make sure to call only > > once. > > > > if !session[:operator_called] > > ... > > session[:operator_called] = true > > end > > > In a method of application controller?It''s just an example, you can add that piece of code in any method, for example: class ApplicationController < ActionController::Base ... before_filter :call_operator_once ... def call_operator_once if !session[:operator_called] fetch_operator() !session[:operator_called] = true end end end -- 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.
On 24 February 2011 19:40, Exequiel Fuentes <efulet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Thu, Feb 24, 2011 at 4:29 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> On 24 February 2011 17:48, exequiel <efulet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> But if I want fetch the operator only once at application startup? >> > >> > I guess you can set up a session variable to make sure to call only >> > once. >> > >> > if !session[:operator_called] >> > ... >> > session[:operator_called] = true >> > end >> > >> In a method of application controller? > > It''s just an example, you can add that piece of code in any method, for > example: > class ApplicationController < ActionController::Base > ... > before_filter :call_operator_once > ... > def call_operator_once > if !session[:operator_called] > fetch_operator() > !session[:operator_called] = true > end > end > endYes but it executes the call_operator before each action of each controller in my application. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Typo error, it should be: def call_operator_once if !session[:operator_called] fetch_operator() session[:operator_called] = true end end Right, it will called before each action of each controller in your application, but only one time the fetch_operator method will be executed. I thought you want to make sure that fetch_operator method is executed at least one time independent of controller. Now, if you want to use a filter method and avoid to call it in other controllers you can use "skip_before_filter :my_method" in each controller. And if you definitely don''t use this method in other controllers, I guess you don''t need define a filter method, just call it as usually in each action that you need it, because any filter that you defined inside the ActionController class will be inherited by other controllers. On Feb 24, 4:44 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yes but it executes the call_operator before each action of each controller in my application.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.