开发者

turning off validation not working

开发者 https://www.devze.com 2023-03-29 11:30 出处:网络
I have the following in my开发者_运维技巧 model: validates:name, :if => :should_validate_name?,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号