Ruby on rails 在带有Desive gem的Rails 4中,在路径中签名后覆盖无效
在ApplicationController中,根据Desive文档,当无法达到case开关时,即使在pry调试控制台中,它也会显示“resource.class==User is true”。我不知道我错过了Rails处理的哪一部分,任何提示都将不胜感激Ruby on rails 在带有Desive gem的Rails 4中,在路径中签名后覆盖无效,ruby-on-rails,devise,Ruby On Rails,Devise,在ApplicationController中,根据Desive文档,当无法达到case开关时,即使在pry调试控制台中,它也会显示“resource.class==User is true”。我不知道我错过了Rails处理的哪一部分,任何提示都将不胜感激 # ApplicationController.rb class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an
# 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
#ApplicationController.rb
类ApplicationController
你已经很接近了。只需使用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
你很接近。只需使用
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
您可以通过创建继承自
designe::sessioncontroller
的sessioncontroller
来解决问题
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
您可以通过创建继承自
designe::sessioncontroller
的sessioncontroller
来解决问题
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
非常感谢。我认为如果你能解释一下定制SessionController而不是在路径中签名后重写ApplicationController背后的想法,你的答案会更好。谢谢!我认为如果你能解释一下定制SessionController背后的想法,而不是在路径中签名之后覆盖ApplicationController,你的答案会更好。