如何在Devise中执行'validates_presence_of'新专栏 - 除了电子邮件&用户模型上的pw?

时间:2012-06-21 09:55:35

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

这是我的User型号:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :timeoutable, 
         :recoverable, :rememberable, :trackable, :validatable,
                 :email_regexp =>  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :confirmed_at, :confirmation_token, :category_ids
    validates_uniqueness_of :email, :case_sensitive => false
    validates_confirmation_of   :password

end

我想要做的是强制实施&#39; category_ids&#39;在激活的&#39;帐户。

我正在做Email Only Sign-up - 这一点很重要,因为这是一个两阶段的过程。用户首次输入其电子邮件地址时,会向他们发送一封激活电子邮件,其中包含指向Confirmations控制器的链接。

Confirmations Controller的视图包含需要验证的其他详细信息。

如果我只是在validates_presence_of型号上定期User,那么当用户最初只提交一个电子邮件地址时 - Rails会失败(我不想要)。< / p>

用户应该:

  1. 输入电子邮件地址
  2. 点击电子邮件中的确认/激活链接
  3. 填写姓名,密码并选择一个类别。
  4. 提交。
  5. 步骤#3,Devise强制执行presence_of_validation for name&amp; PW。但不是我的自定义项category

    我该怎么做?

    感谢。

1 个答案:

答案 0 :(得分:1)

您可以将:on => :update选项传递给validates_presence_of,但不会在创建时验证:

实施例

 validates_presence_of :category_ids, :on => :update