2

我正在使用可确认的设计。我遇到了一个问题,如果用户多次单击确认链接(即,如果用户忘记了他们已经确认或其他什么),他们会收到一个令人讨厌的错误:

NoMethodError at /confirmation
undefined method `new_user_registration_path' for  <ActionDispatch::Routing::RoutesProxy:0x007fe739a40510>

这是确认链接:

<%= link_to 'Confirm my account', confirmation_url(@user, host: @host, confirmation_token: @user.confirmation_token) %>

理想的行为是 flash[:alert] = "You've already Confirm" + 重定向到登录页面。

我尝试创建自己confirmations_controller.rb的路线并弄乱路线,但没有任何成功。

提前感谢您的任何帮助!

4

2 回答 2

2

默认情况下,Devise 将用户重复到达确认页面(使用相同的确认令牌)视为错误条件。我认为避免该消息的最简单方法是覆盖pending_any_confirmationDevise::Models::Confirmable 中的方法。从该方法中删除将其标记为错误条件的行应该可以解决问题。

假设你有一个用户模型,在你的app/models/user.rb文件中,插入这个:

private

def pending_any_confirmation
  if (!confirmed? || pending_reconfirmation?)
    yield
# else
#   self.errors.add(:email, :already_confirmed)
#   false
end

注意:我正在显示注释行,以便您可以看到 Devise 的原始方法中究竟发生了什么变化。相反,您的方法可能只有一行:yield if (!confirmed? || pending_reconfirmation?)

于 2016-06-22T20:18:21.763 回答
1

在 SO 上找到一个几乎相同的问题后,我自己解决了这个问题:How redirect 'Confirmation token invalid' on Devise

将来自设计的批发复制confirmations_controller.rb到我的应用程序中不起作用,但要求supercreate。然后指定一个新的show动作有效。

class ConfirmationsController  < Devise::ConfirmationsController

  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
      flash[:notice] = "Your account is already confirmed. Please login."
      redirect_to '/login'
    end
  end

end
于 2013-12-02T00:54:59.727 回答