[ACCEPTED]-redirect to login page if user not logged in-ruby-on-rails
Accepted answer
Use a before_filter. Place the following in your application 4 controller.
# application_controller.rb
before_filter :require_login
private
def require_login
unless current_user
redirect_to login_url
end
end
The code above assumes you have 3 defined a method current_user
which returns the user 2 record when the user is logged in.
Then, in 1 your login controller
skip_before_filter :require_login
If you want login to be redirected only 1 on particular controllers, then use:
skip_before_filter :require_login, :only=>[:edit,:update]
or:
skip_before_filter :require_login, :except=>[:new,:create]
In your action_controller.rb as a before_filter.
0
If you're using devise you can add a before_action 1 helper to your controller
before_action :authenticate_user!
https://github.com/heartcombo/devise#controller-filters-and-helpers
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.