I have custom controller action for activating a new customer acount in a webshop. I have a model called customer.rb. The model generates a key and sends the customer an activation link. The link is routed to the customers_controller.rb and the following action:
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
I first tried it without @customer.save but the record did not update. I have raised before and after @customer.activated = true and it all seems fine. The only problem is that the record does does not update. I also checked a possible attar_accessible problem, but that’s not it (I shut of attr_accessible entirely to check). Any ideas? Thanx in advance!
*Edit: checked value of @customer.save, returns false…
*Edit:
The problem seems to be the password validation on the Customer model. The validations look as follows:
# 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
Any ideas how to skip validations on this specific controller action? Thanx for thinking with me!
You can use conditional validation which is the best practice in this case.
Here is a railscast on that http://railscasts.com/episodes/41-conditional-validations
You can then use some code like this:
You then need to set updating_password to false if you want to skip validation.