Ruby on rails Rails自定义控制器操作不保存
我有自定义控制器操作,用于在网店中激活新客户帐户。我有一个叫做Ruby on rails Rails自定义控制器操作不保存,ruby-on-rails,controller,updates,Ruby On Rails,Controller,Updates,我有自定义控制器操作,用于在网店中激活新客户帐户。我有一个叫做customer.rb的模型。该模型生成一个密钥并向客户发送一个激活链接。链接被路由到客户\控制器.rb并执行以下操作: def activate_customer @customer = Customer.find(params[:customer_id]) if @customer.present? if @customer.activation_key == params[:activation_key]
customer.rb
的模型。该模型生成一个密钥并向客户发送一个激活链接。链接被路由到客户\控制器.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\u accessible
问题,但不是这个问题(我完全关闭attr\u 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
有没有关于如何跳过此特定控制器操作的验证的想法?谢谢你和我一起思考 您可以使用条件验证,这是本例中的最佳实践
这是上面的一张火车票
然后可以使用如下代码:
attr_accessor :updating_password
validates_confirmation_of :password, :if => should_validate_password?
def should_validate_password?
updating_password
end
如果要跳过验证,则需要将更新_密码设置为false
@customer.updating_password = false
@customer.save
您首先通过puts命令检查@customer是否具有某些值。您的客户模型上的验证可能失败。您是否检查了@customer.save
返回的内容?它是否返回false
?好的,检查它,它返回false…您是否检查了@customer.errors?也许有些验证失败了。谢谢!我认为问题与密码字段有关。它验证密码的存在和确认,我认为这会阻止保存。Wich给我带来了跳过单个控制器操作的验证的问题。我已编辑了此问题的问题;)谢谢你。很好的简单解决方案!不应该是“:if=>:应该验证密码吗?”?不应该,因为我们正在调用一个方法