Code sample: class SomeController < ApplicationController before_filter do |c| add_crumb "Blah", "/blah" #breadcrumbs plugin redirect_to :controller => "foo", :action => "bar" unless c.send (:has_package?) end # Rest of the controller... private def has_package? # A bunch of logic work to check to make sure # somebody''s session contains certain variables # returns ONLY true or false. end The problem I''m having is that I''m getting "undefined method" errors for redirect_to. This confuses me since SomeController inherits from ApplicationController which inherits from ActionController::Base. Any idea what''s causing this problem? I''m thinking this is a scope issue somehow, but I''m not sure why it''s failing like this. Thanks.
One thing I forgot to mention. I''ve tweaked it so that I CAN get it to work by calling the redirect_to method while inside has_package? - so basically it looks like this: def has_package? # if conditions redirect_to :controller => "foo", :action => "bar" # else redirect_to :controller => "foo", :action => "bar" # end end So before_filter is calling this method which CAN access redirect_to as inherited from ActionController::Base. I''m just confused as to why I have some obvious inheritance issues here. On Apr 23, 7:45 pm, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Code sample: > > class SomeController < ApplicationController > before_filter do |c| > add_crumb "Blah", "/blah" #breadcrumbs plugin > redirect_to :controller => "foo", :action => "bar" unless c.send > (:has_package?) > end > > # Rest of the controller... > > private > > def has_package? > # A bunch of logic work to check to make sure > # somebody''s session contains certain variables > # returns ONLY true or false. > end > > The problem I''m having is that I''m getting "undefined method" errors > for redirect_to. This confuses me since SomeController inherits from > ApplicationController which inherits from ActionController::Base. Any > idea what''s causing this problem? I''m thinking this is a scope issue > somehow, but I''m not sure why it''s failing like this. > > Thanks.
Frederick Cheung
2009-Apr-24 07:47 UTC
Re: Undefined method "redirect_to" in before_filter
On Apr 24, 2:50 am, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> One thing I forgot to mention. I''ve tweaked it so that I CAN get it > to work by calling the redirect_to method while inside has_package? - > so basically it looks like this: > > def has_package? > # if conditions > redirect_to :controller => "foo", :action => "bar" > # else > redirect_to :controller => "foo", :action => "bar" > # end > end > > So before_filter is calling this method which CAN access redirect_to > as inherited from ActionController::Base. I''m just confused as to why > I have some obvious inheritance issues here. >with a before_filter using the block syntax self is the SomeController class itself, but redirect_to is an instance method. Much as you have to call your has_package method on the controller instance that is yielded to the block you must also call redirect_to on that object. Fred
So then would the syntax be: c.redirect_to ... ? Thanks Fred - for the record, you''re the man ;-) On Apr 24, 1:47 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 24, 2:50 am, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > One thing I forgot to mention. I''ve tweaked it so that I CAN get it > > to work by calling the redirect_to method while inside has_package? - > > so basically it looks like this: > > > def has_package? > > # if conditions > > redirect_to :controller => "foo", :action => "bar" > > # else > > redirect_to :controller => "foo", :action => "bar" > > # end > > end > > > So before_filter is calling this method which CAN access redirect_to > > as inherited from ActionController::Base. I''m just confused as to why > > I have some obvious inheritance issues here. > > with a before_filter using the block syntax self is the SomeController > class itself, but redirect_to is an instance method. Much as you have > to call your has_package method on the controller instance that is > yielded to the block you must also call redirect_to on that object. > > Fred
Frederick Cheung
2009-Apr-24 10:05 UTC
Re: Undefined method "redirect_to" in before_filter
On Apr 24, 10:46 am, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So then would the syntax be: > > c.redirect_to ... >Yup (unless the method was protected/private in which case you could use send, but i don;t think it is) Fred> ? > > Thanks Fred - for the record, you''re the man ;-) > > On Apr 24, 1:47 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Apr 24, 2:50 am, Phoenix Rising <PolarisRis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > One thing I forgot to mention. I''ve tweaked it so that I CAN get it > > > to work by calling the redirect_to method while inside has_package? - > > > so basically it looks like this: > > > > def has_package? > > > # if conditions > > > redirect_to :controller => "foo", :action => "bar" > > > # else > > > redirect_to :controller => "foo", :action => "bar" > > > # end > > > end > > > > So before_filter is calling this method which CAN access redirect_to > > > as inherited from ActionController::Base. I''m just confused as to why > > > I have some obvious inheritance issues here. > > > with a before_filter using the block syntax self is the SomeController > > class itself, but redirect_to is an instance method. Much as you have > > to call your has_package method on the controller instance that is > > yielded to the block you must also call redirect_to on that object. > > > Fred