turning off validation not working
I have the following in my开发者_运维技巧 model:
validates :name, :if => :should_validate_name?,
:presence => {:message => "Enter your name"},
:length => { :maximum => 50 },
:allow_blank => true
def should_validate_name?
validating_name || new_record?
end
In my controller I have the following:
def create
@user = User.new(params[:user])
@user.validating_name = false
if @user.save
else
render :action => 'new'
end
end
I don't want to validate for the presence of a name at this point and wish to turn it off.
I thought the code above would work but it doesn't. I don't know why.
You're in the create
action, creating a new record. So new_record?
will be true, even if validating_name
isn't.
精彩评论