[英]In Rails 4 with devise gem, overriding after_sign_in_path_for is not effective
根據設計文檔,在ApplicationController中,“ 如何:成功登錄和注銷時重定向到特定頁面” ,即使在撬調試控制台中也無法到達時切換大小寫,它顯示'resource.class ==用戶為true '。 我不知道我錯過了Rails處理的哪一部分,任何提示將不勝感激!
# ApplicationController.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protected
def after_sign_in_path_for(resource)
# check for the class of the object to determine what type it is
binding.pry
case resource.class
when User
puts "user redirect ==== "
return session.delete(:return_to) || current_user_path
else
puts "super call ....."
super
end
end
end
你很親密 只需使用resource.class.name
獲取資源類名稱,以便您可以將其與諸如'User'
類的字符串進行比較,這不過是您的類名稱。
def after_sign_in_path_for(resource)
# check for the class of the object to determine what type it is
binding.pry
case resource.class.name #=>this would return the class name i.e 'User'
when 'User'
puts "user redirect ==== "
return session.delete(:return_to) || current_user_path
else
puts "super call ....."
super
end
end
您可以通過創建從Devise::SessionsController
繼承的SessionsController
來解決。
class SessionsController < Devise::SessionsController
skip_before_filter :authenticate_user!
def create
user = User.find_for_database_authentication(email: params[:session][:email])
if user && user.valid_password?(params[:session][:password])
sign_in user
redirect_to session.delete(:return_to) || '/authorized'
else
redirect_to '/sign_in'
end
end
def destroy
sign_out :user
redirect_to '/signed_out'
end
end
像這樣在您的routes.rb
指向它:
devise_for :users, controllers: {sessions: 'sessions'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.