How to show a login modal in Rails if a user tries to access an authorized section
How would you implement a modal window saying "In order you to access this area you have to be开发者_运维百科 logged in.." in combination with a login form below it every time a user tries to access authorized content. I'm using Rails 3, Devise and CanCan.
My first thoughts were do put it somewhere in the application_controller.rb:
# application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = "Access denied."
redirect_to root_url
end
I'm not using a modal window, but this works
rescue_from CanCan::AccessDenied do |exception|
unless anybody_signed_in?
authenticate_user!
else
flash[:alert] = "Access denied."
redirect_to root_url
end
end
It will redirect the use to the login form. When the user provides his credentials, he'll be shown the page he wanted to access.
精彩评论