Forum: Ruby on Rails Undefined method "redirect_to" in before_filter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Phoenix R. (Guest)
on 2009-04-24 05:45
(Received via mailing list)
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.
Phoenix R. (Guest)
on 2009-04-24 05:51
(Received via mailing list)
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.
Frederick C. (Guest)
on 2009-04-24 11:48
(Received via mailing list)
On Apr 24, 2:50 am, Phoenix R. <removed_email_address@domain.invalid> wrote:
> 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
Phoenix R. (Guest)
on 2009-04-24 13:47
(Received via mailing list)
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 C. <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2009-04-24 14:06
(Received via mailing list)
On Apr 24, 10:46 am, Phoenix R. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.