I have a controller with a couple of actions and one model.
The model is as so:
validates_presence_of :name, :email, :name
validates_presence_of :password, :password_confirmation
The first action is create and is called by the administrator.
With this action I do not want to validate the password as this is
The second action allows the user change their password.
With this action I do not want to validate the email, as there is no
However the problem lies in the fact that the Administrator can also
the name and email.
Therefore I am unable to use the :on => create or :on => update, as
validation is required on update for the user,
while email and name validation is required on update and create by the
administrator. Quite the pickle! Is there a way of
restricting validation to a controller/action? Therefore I can avoid
calling certain validations at certain actions?
a simple suggestion on the top of my head would be to create two
different actions for the admin / user. instead of :update, make two
identical actions, with different names such as :update_for_user &&
:update_for_admin and then just set the
validates_* to :update_for_user
in the correct cases.
I think that’s exactly what I was looking for, thank you very much…
This is how I did it. Sounds similar to what you need.
class Customer < ActiveRecord::Base
validates_presence_of :current_password, :on => :update, :if =>
@password_change = true
self.attributes = attributes
errors.add(:current_password, "is incorrect") unless
encrypt(current_password) == crypted_password
class AccountController < ApplicationController
@customer = @current_customer
flash[:notice] = “Your password has been changed”
I tried that, it doesn’t work as there are no such parameters for a