开发者

Cannot filter logged in users on the application_controller

I'm working with Ruby on Rails 2.3.8 and I want users to login before accesing any content from the website.

I've written the following in the application_controller

before_filter :login_required

When the app starts, I'm getting an error in the browser (not even from the application itself) saying it failed to redirect the page. And I see in the URL that says http://localhost:3000/session/new but it won't work.

Then, I've tried to place that filter in another controller, for example in dashboard_controller, which is the home page, and it worked fine (but it jus开发者_开发百科t works for this controller only, and I want it to filter in the entire app).

I then started thinking, and I actually think that the filter is being executed before the entry for one of these two lines are created in routes file:

map.login    '/login',    :controller => 'sessions', :action => 'new'
map.signup   '/signup',   :controller => 'users',    :action => 'new'

Any help will be appreciated.


I assume you're redirecting without excluding the controller action that you don't want to redirect from. That is, your before filter is being triggered from the login page.

If so, skip the before_filter in your session controller

skip_before_filter :login_required, :only => [:login, :create]


You don't want to filter every request, only the ones requiring a logged in user. So put the before_filter in each controller where it is required instead of in ApplicationController. Yes, this means there will be some duplication (Oh, the horror!), but the intent of the code will be clearer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜