Ух ты, только что понял, что devise (3.5.2)
делает все это сам за кулисами (вокруг Devise::SessionsController#new
действие), никаких дополнительных модификаций контроллера не требуется.
Если вынужно явно store
/ get
предыдущий location
, см. мой предыдущий ответ:
В настоящее время ( осень 2015 ) есть более сексуальный способ сделать это:
Devise::Controllers::StoreLocation#store_location_for
:
# Stores the provided location to redirect the user after signing in.
# Useful in combination with the `stored_location_for` helper.
store_location_for :user, dashboard_path
redirect_to user_omniauth_authorize_path :facebook
Devise::Controllers::StoreLocation#stored_location_for
:
# Returns and delete (if it's navigational format) the url stored in the session for
# the given scope. Useful for giving redirect backs after sign up:
redirect_to stored_location_for(:user) || root_path
Методы обрабатывают связанные session
удаление ключа и значения после чтения, все что вам нужно, этоукажите ключ :resource
(:user
в примере выше) и путь к хранилищу (dashboard_path
в примере выше).Подробности смотрите в source .
Что касается фактического ответа, то он будет примерно таким:
class ApplicationController < ActionController::Base
rescue_from CanCan::AccessDenied, with: :access_denied
# ...
private
def access_denied(exception)
store_location_for :user, request.path
redirect_to user_signed_in? ? root_path : new_user_session_path, alert: exception.message
end
def after_sign_in_path_for(resource)
stored_location_for(:user) || root_path
end
end