【问题标题】:Error-undefined method `add_error_on' for ConfirmationsController:Class-rails4-deviseConfirmationsController:Class-rails4-devise 的错误未定义方法“add_error_on”
【发布时间】:2014-08-07 06:56:48
【问题描述】:

我正在使用 Rails 4 并设计 3.2。在注册时,激活/确认链接会通过电子邮件发送给新注册的用户。我已经从this article in the Devise wiki 实现了这个。

每当我输入相同的密码时,他们就可以毫无问题地登录,但是当我在第一次尝试时输入密码不匹配错误并在第二次尝试中输入相同的密码时,它就不起作用了。

这是出现的错误

NoMethodError in ConfirmationsController#update
undefined method `add_error_on' for ConfirmationsController:Class

确认控制器

class ConfirmationsController < Devise::ConfirmationsController

  skip_before_filter :require_no_authentication
  skip_before_filter :authenticate_user!

  # PUT /resource/confirmation
  def update
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        @confirmable.attempt_set_password(params[:user])
        if @confirmable.valid? and @confirmable.password_match?
          do_confirm
        else
          do_show
          @confirmable.errors.clear #so that we wont render :new
        end
      else
        self.class.add_error_on(self, :email, :password_already_set)
      end
    end

    if !@confirmable.errors.empty?
      render 'devise/confirmations/new'
    end
  end

  # GET /resource/confirmation?confirmation_token=abcdef
  def show
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        do_show
      else
        do_confirm
      end
    end
    if !@confirmable.errors.empty?
      self.resource = @confirmable
      render 'devise/confirmations/new'
    end
  end

  protected

  def with_unconfirmed_confirmable
    original_token = params[:confirmation_token]
    confirmation_token = Devise.token_generator.digest(User, :confirmation_token, original_token)
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
    if !@confirmable.new_record?
  
      @confirmable.only_if_unconfirmed {yield}
    end
  end

  def do_show
    @confirmation_token = params[:confirmation_token]
    @requires_password = true
    self.resource = @confirmable
    render 'devise/confirmations/show'
  end

  def do_confirm
    @confirmable.confirm!
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, @confirmable)
   end
 end

请帮忙。谢谢。

【问题讨论】:

  • 我遇到了同样的错误,有什么更新吗?

标签: ruby-on-rails devise devise-confirmable


【解决方案1】:

该 wiki 文章中更新操作的错误案例已过时。 Devise 中不再有add_error_on 方法。相反,您可以只使用基本的 ActiveRecord 错误对象:

@confirmable.errors.add(:email, :password_already_set)

然后,您仍然会遇到问题,因为它需要将@confirmable 分配给resource 以使默认视图正常工作(wiki 示例对所有其他错误呈现都这样做,而不是这个一):

if !@confirmable.errors.empty?
  self.resource = @confirmable
  render 'devise/confirmations/new'
end

【讨论】:

  • 我仍然收到错误消息-“缺少电子邮件翻译:en.activerecord.errors.models.user.attributes.email.password_already_set”。它要求我们重新发送确认链接。单击该链接时,即使第二次输入的密码不匹配,用户也会登录。
  • 这解决了这个问题,但现在这个页面转到“重新发送确认说明”页面..任何想法,为什么它会出现在重新发送确认页面上?
  • 翻译错误是因为默认情况下Devise不包含“password_already_set”的错误消息。您可以将其添加到 devise.en.yml(或您的语言环境文件)。关于密码不匹配的问题,你使用的是可验证模块吗?
  • 我删除了这个条件 - 如果 @confirmable.has_no_password?.. 一切正常。谢谢你。你能告诉我这个条件(如果@confirmable.has_no_password?)在注册过程中有用或适用吗?
  • @LouisSimoneau 感谢您提供的信息。看起来 Rails 4 文档对于整个事情来说已经过时了,可能需要一些关注。
猜你喜欢
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多