我使用 devise :confirmable 配置了我的 Rails 应用程序,注册后用户会收到一封包含确认链接的电子邮件。当用户第二次单击此链接时,令牌无效,这是我的应用程序中的预期行为。但是,它将用户发送到位于“app\views\devise\confirmations\new.html.erb”的页面,并显示错误“确认令牌无效”,我想要的是显示该错误(),但在我的登录页面中。 (实际上有两种可能的状态: a)token过期,用户未确认,用户必须再次索要token; b) 令牌已过期且用户已确认,因此用户必须登录)
为此,我继承了 Devise::ConfirmationsController,创建了自己的ConfirmationsController 来覆盖 show 方法,这样如果出现任何错误,我可以重定向到登录页面,如下所示:
class ConfirmationsController < Devise::ConfirmationsController
def after_confirmation_path_for(resource_name, resource)
super
end
def new
super
end
def after_resending_confirmation_instructions_path_for(resource_name)
super
end
def create
super
end
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_navigational_format?
sign_in(resource_name, resource)
respond_with_navigational(resource){ redirect_to
after_confirmation_path_for(resource_name, resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render 'devise/sessions/new' }
end
end
end
在我的routes.rb 中我有这样的配置:
get "confirmations/show"
devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}, :controllers => {:confirmations => 'confirmations'}
(...)
它正确重定向到登录页面,但我can't将 resources.errors 中存在的错误传递到登录页面,无论如何hard我尝试一下。我怎么解决这个问题?
顺便说一句,我的用户模型是这样配置的:
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable