Forum: Ruby on Rails controller before_filter issues

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.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-11-16 17:17
(Received via mailing list)
Hi,

Some controller filters:

   before_filter :require_logged_in,  :only =>  [:create, :new,
:confirm]
   before_filter :init_players,  :only =>  [:create, :new, :confirm]

The first one, you guessed it, redirects a user if he's not logged in.

Now when I hit this page and I'm not logged in, the 2nd filter gets
executed and throws an error, because it depends on the user being
logged in. I don't really want to do an extra check in the init_players
method, it's not very DRY..

Any ideas?

Jeroen
4f9f63a237d544bb350e0f2fb53a7f24?d=identicon&s=25 lgoats (Guest)
on 2005-11-16 19:48
(Received via mailing list)
You probably need to add a return at the end of your redirect:

redirect(....) and return

-Larry
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-11-16 20:24
(Received via mailing list)
Hi !

2005/11/16, Jeroen Houben <jeroen@terena.nl>:
> Now when I hit this page and I'm not logged in, the 2nd filter gets
> executed and throws an error, because it depends on the user being
> logged in. I don't really want to do an extra check in the init_players
> method, it's not very DRY..

When you redirect, do you prevent further filter chain execution by
returning false ?

You would do it this way:
def require_logged_in
  # check authentication
  # user not authenticated, we need to redirect
  redirect_to :login_url
  return false # prevents further filter chain execution (aborts
request)
end

Hope that helps !
François
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-11-17 10:47
(Received via mailing list)
Francois Beausoleil wrote:
> When you redirect, do you prevent further filter chain execution by
> Hope that helps !
It helps!

Jeroen
This topic is locked and can not be replied to.