rails跳过验证

2010 - 12 - 21

rails validation

文章分类:Ruby编程
rails3中的验证,以下方法会触发验证
Java代码
  1. create  
  2. create!  
  3. save  
  4. save!  
  5. update  
  6. update_attributes  
  7. update_attributes!  
create
create!
save
save!
update
update_attributes
update_attributes!

以下方法则会跳过验证,将数据保存到数据库中
Java代码
  1. decrement!  
  2. decrement_counter  
  3. increment!  
  4. increment_counter  
  5. toggle!  
  6. update_all  
  7. update_attribute  
  8. update_counters  
decrement!
decrement_counter
increment!
increment_counter
toggle!
update_all
update_attribute
update_counters

当使用
Java代码
  1. save(:validate => false)  
save(:validate => false)

验证也会被跳过。

Java代码
  1. validates_acceptance_of  
validates_acceptance_of
必须接受(多用于霸王条款)

Java代码
  1. class Library < ActiveRecord::Base     
  2. has_many :books    
  3. validates_associated :books  
  4. end   
class Library < ActiveRecord::Base   
has_many :books  
validates_associated :books
end 



Java代码
  1. validates_confirmation_of :password(多用于验证两次密码)  
validates_confirmation_of :password(多用于验证两次密码)


Java代码
  1. class Account < ActiveRecord::Base   validates_exclusion_of :subdomain, :in => %w(www),     :message => "Subdomain %{value} is reserved."  
  2. end (用于验证是否包含此www)  
class Account < ActiveRecord::Base   validates_exclusion_of :subdomain, :in => %w(www),     :message => "Subdomain %{value} is reserved."
end (用于验证是否包含此www)


Java代码
  1. class Product < ActiveRecord::Base   validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/,     :message => "Only letters allowed"end   
  2. (格式验证)  
class Product < ActiveRecord::Base   validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/,     :message => "Only letters allowed"end 
(格式验证)


上传时验证文件类型:
Java代码
  1. # 验证文件后缀    
  2. validates_format_of :photo, :with => %r{\.(gif|png|jpg)$}i, :message => "must be a URL for a GIF, JPG, or PNG image"  
  3. # 如果使用file_column,则可以用以下方法  
  4. validates_file_format_of :photo, :in => ["gif""png""jpg"]  

 

 

 

最后我是用p.save false来实现跳过验证的

hod

update_attributes

Importance_4
update_attributes(attributes)  public

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

Register or log in to add new notes.
August 27, 2008
15 thanks
Only attr_accessible attributes will be updated

If your model specified attr_accessible attributes, only those attributes will be updated.

Use attr_accessible to prevent mass assignment (by users) of attributes that should not be editable by a user. Mass assignment is used in create and update methods of your standard controller.

For a normal user account, for example, you only want login and password to be editable by a user. It should not be possible to change the status attribute through mass assignment.

  class User < ActiveRecord::Base
    attr_accessible :login, :password
  end

So, doing the following will merrily return true, but will not update the status attribute.

  @user.update_attributes(:status => 'active')

If you want to update the status attribute, you should assign it separately.

  @user.status = 'active'
  save
August 14, 2008
4 thanks
Calls attribute setter for each key/value in the hash

This is a convenience to set multiple attributes at the same time. It calls the "setter" method

  self.attribute=(value)

for each key in the hash. If you have overridden the setter to add functionality, it will be called.

This also allows you to create non-table attributes that affect the record. For instance, a full_name=() method could parse the string and set the first_name=() and last_name() accordingly.

June 17, 2009
3 thanks
Skipping validation

Unlike the save method, you can’t pass false to update_attributes to tell it to skip validation. Should you wish to do this (consider carefully if this is wise) update the attributes explicitly then call save and pass false:

  @model_name.attributes = params[:model_name]
  @model_name.save false
posted on 2011-01-27 22:38  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2011/01/27/1946508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值