在ApplicationController中,根据设计文档How To: Redirect to a specific page on successful sign in and sign out,无法访问案例切换,即使在pry调试控制台中,也显示'resource.class == User is 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
答案 0 :(得分:1)
你非常接近。只需要使用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
只是您的类名。
etc/profile
答案 1 :(得分:1)
您可以通过创建继承自SessionsController
。
Devise::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'}