浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
2010-12-21

rails validation

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

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

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

验证也会被跳过。

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

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



Java代码
  1. 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)  


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


上传时验证文件类型:
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  阅读(848)  评论(0编辑  收藏  举报