0

我有用于在网上商店激活新客户帐户的自定义控制器操作。我有一个模型叫customer.rb. 该模型生成一个密钥并向客户发送一个激活链接。该链接被路由到customers_controller.rb以下操作:

def activate_customer
  @customer = Customer.find(params[:customer_id])
    if @customer.present?
     if @customer.activation_key == params[:activation_key]
       @customer.activated = true
       @customer.save
       redirect_to :cart
     else
       redirect_to :root
     end
    else
     redirect_to :root
    end
end

我第一次尝试没有@customer.save但记录没有更新。我之前和之后都提出过,@customer.activated = true这一切似乎都很好。唯一的问题是记录没有更新。我还检查了一个可能的attar_accessible问题,但不是这样(我attr_accessible完全关闭检查)。有任何想法吗?提前谢谢!

*编辑:检查值@customer.save,返回false...

*编辑:

问题似乎是客户模型上的密码验证。验证如下所示:

# Validations
validates_presence_of :title, :country, :zipcode, :city, :street, :number, :first name, :lastname
validates :email, :presence => true, :uniqueness => true, :email => true
validates_confirmation_of :password, :unless => :validate?
validates_presence_of :password

任何想法如何跳过此特定控制器操作的验证?感谢您和我一起思考!

4

1 回答 1

0

您可以使用条件验证,这是在这种情况下的最佳做法。

这是关于http://railscasts.com/episodes/41-conditional-validations的 railscast

然后,您可以使用如下代码:

attr_accessor :updating_password

validates_confirmation_of :password, :if => should_validate_password?

def should_validate_password?
  updating_password
end

然后,如果您想跳过验证,则需要将 updates_password 设置为 false。

@customer.updating_password = false
@customer.save
于 2012-07-12T10:33:08.543 回答