Deviseのログイン後のページ先をユーザーフレンドリーにする
せっかくログインしたのに、トップページに戻ると、なんだこれ?ってなってしまいます。
それを回避する方法です。
class ApplicationController < ActionController::Base before_action :store_user_location!, if: :storable_location? private def storable_location? request.get? && is_navigational_format? && !devise_controller? && !request.xhr? end def store_user_location! # :user is the scope we are authenticating store_location_for(:user, request.fullpath) end
storable_action
でGET/Devise/Ajaxじゃないなどの判定をします。
その場合のみ、sessionでurlを保存しています。
class Users::SessionsController < Devise::SessionsController include Cookie protected def after_sign_in_path_for(resource_or_scope) stored_location_for(resource_or_scope) || super end end
あとはsession後にそれがあるかどうかを判定します。
参考
wikiが充実していて、本当に助かる。
ここから何かをカスタムする場合は、storable_location?
をいじるか、after_sign_in_path_for
で条件分岐でいじるかになると思います。