ここでは認証にDevise Gemを使用しています。ログインせずにページを開きたい場合、sign_inページにリダイレクトされ、サインイン後にユーザーが開こうとしているページに戻ります。私はDevise after_sign_in_path_for によるリダイレクトループ私の問題へのリンクですが、うまくいきません。
def after_sign_in_path_for(resource)
params[:next] || super
end
開きたいページにリダイレクトされません。たとえば、「127.0.0.1:3000/post/2/edit」を開きたい場合、サインインしてもこのページに戻りません。
ベストアンサー1
探すのに最適なリソースは、公式リポジトリ/wiki/問題、そして SO です。あなたが見つけた答えは古くなっています。
ApplicationControllerに以下を追加するだけですバージョン 3.2.1 以降の場合:
# This example assumes that you have setup devise to authenticate a class named User.
class ApplicationController < ActionController::Base
before_action :store_user_location!, if: :storable_location?
# The callback which stores the current location must be added before you authenticate the user
# as `authenticate_user!` (or whatever your resource is) will halt the filter chain and redirect
# before the location can be stored.
before_action :authenticate_user!
private
# Its important that the location is NOT stored if:
# - The request method is not GET (non idempotent)
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an
# infinite redirect loop.
# - The request is an Ajax request as this can lead to very unexpected behaviour.
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
end
サインイン後にリダイレクトするには、このメソッドをオーバーライドする必要があります。
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || super
end