Hello, I''m using this in almost all methods of my Customer Controler if params[:type]==''in'' # handle incoming customers kind = ::KIND_CUSTOMER else # handle outgoing customers kind = ::KIND_SUPPLIER end I don''t want to copy&paste in all of the methods of this control, so I tried to put it at the top of the controller, to be available to all methods, but it doesn''t work ... This is what I want, but it doesn''t work: -------------------------------------------- class CustomerController < ApplicationController around_filter :login_required if params[:type]==''in'' # handle incoming customers kind = ::KIND_CUSTOMER else # handle outgoing customers kind = ::KIND_SUPPLIER end erro => undefined local variable or method `params'' for CustomerController:Class I tried to put in a helper, but the helper''s methods are not recognized in the controller ... thanks, raimon -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-05 11:37 UTC
Re: where to put code for all methods in a Controller
On 5 Dec 2007, at 10:33, Raimon Fs wrote:> > Hello, > > > I''m using this in almost all methods of my Customer Controler > > if params[:type]==''in'' > # handle incoming customers > kind = ::KIND_CUSTOMER > else > # handle outgoing customers > kind = ::KIND_SUPPLIER > end > >add a before_filter that does this (you''ll want to use an instance variable for kind instead of a local variable Fred
Frederick Cheung wrote:> On 5 Dec 2007, at 10:33, Raimon Fs wrote: > >> # handle outgoing customers >> kind = ::KIND_SUPPLIER >> end >> >> > add a before_filter that does this (you''ll want to use an instance > variable for kind instead of a local variable > > Fredok, I added this to the customer controll: def get_kind if params[:type]==''in'' # handle incoming customers @kind = ::KIND_CUSTOMER else # handle outgoing customers @kind = ::KIND_SUPPLIER end end and at the top, just: before_filter :get_kind and where I need it, I just put @kind tahnks, it''s working, what I don''t understand, is why I have to create a method and call it with a before_filter, instead of simply adding the code at the top ... regards, raimon -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-05 12:11 UTC
Re: where to put code for all methods in a Controller
On 5 Dec 2007, at 11:59, Raimon Fs wrote:> > Frederick Cheung wrote: >> On 5 Dec 2007, at 10:33, Raimon Fs wrote: >> >>> # handle outgoing customers >>> kind = ::KIND_SUPPLIER >>> end >>> >>> >> add a before_filter that does this (you''ll want to use an instance >> variable for kind instead of a local variable >> >> Fred > > ok, I added this to the customer controll: > > def get_kind > > if params[:type]==''in'' > # handle incoming customers > @kind = ::KIND_CUSTOMER > else > # handle outgoing customers > @kind = ::KIND_SUPPLIER > end > > end > > and at the top, just: > > before_filter :get_kind > > > and where I need it, I just put @kind > > tahnks, it''s working, what I don''t understand, is why I have to > create a > method and call it with a before_filter, instead of simply adding the > code at the top ... >''Adding code'' at the top tries to execute the code in the context of the controller class. Code added at the top level is run when the controller is loaded, ie once only - (in production classes aren''t reloaded). You need something that happens once per request, is run by an instance of the controller at a particular point in time (after the request has been received by the controller, before the action code is run), so it can''t possibly be achieved by class level stuff Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> ''Adding code'' at the top tries to execute the code in the context of > the controller class. > Code added at the top level is run when the controller is loaded, ie > once only - (in production classes aren''t reloaded). You need > something that happens once per request, is run by an instance of the > controller at a particular point in time (after the request has been > received by the controller, before the action code is run), so it > can''t possibly be achieved by class level stuff > > Fredthanks, I understand now ... rai -- 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 -~----------~----~----~----~------~----~------~--~---