Rails中validates及各种用法
首先是不为空validates :name,presence:{message:'blank is not allowed'}
唯一性:validates :name,uniqueness:{message:'already exist!',case_sensitive: false}#case_sensitive区分大小写
长度:validates :name,length:{in: 6..20,message:'must in 6-20 byte'}
格式:validates :name,format:{with: /[a-z]/,message:'must contain at least one lower case character'}
范围:validates :country,inclusion:{in:['China','USA','Jpan','Mexico'],message:'must one of China USA Jpan Mexico '}
数字:validates :graduate,numericality:{allow_nil: true,greater_than: 1920,less_than_or_equal_to:Time.now.year}
validates :description,presence:true,if: :require_description_presence? def require_description_presence? self.can_send_mail end
这个validates :description,presence: true只有在require_description_presence?方法返回true的时候才会起作用
validate :description_length_words def description_length_words unless self.description.blank? then num_words = self.description.split.length if num_words < 5 then self.errors.add(:description,'must be at least 5 words long') elsif num_words > 50 self.errors.add(:description,'must be at most 50 words long') end end end
相关推荐
0.4.0 版还添加了 Rails 3 支持(自动使用适当的版本)。 0.5.0 版引入了一个新选项 - :both并更改了错误附加的默认行为。 0.5.3 的变化 清理代码,整个库现在在一个文件中,包含 Rails 2 和 Rails 3 的单独模块。 ...
验证格式 validates_formatting_of gem添加了几种便捷的方法来验证Rails应用程序中的内容,例如电子邮件...使用validates_formatting_of就像在模型中使用Rails的内置验证方法一样简单。 class User < ActiveRecord
用法使用ActiveRecord class Foo < ActiveRecord :: Base # validate that attribute :bar is a String validates_type :bar , :string # validate that attribute :baz is an Integer with a custom error
安装在Gemfile中: gem 'rails-uploader'在航线上: mount Uploader :: Engine => '/uploader' 迁移ActiveRecord: $ bundle exec rails g uploader:install用法存储上传文件的架构(cancan集成): class Asset <...
安装将此添加到您的Gemfile : gem "validate_url" 或自己安装: sudo gem install validate_url用法使用ActiveRecord class Pony < ActiveRecord :: Base # standard validation validates :homepage , url : ...
另外,acts_as_tenant: 使用子域设置当前租户,或允许您自己传递当前租户防止针对房客范围界定的各种类型的混乱向租户添加验证唯一性的方法validates_uniqueness_to_tenant 设置一个包含当前租户的助手方法注意:...
使用Ruby on Rails中的Kickbox.io API验证电子邮件,如果Kickbox.io API由于任何原因失败,则退回到基本语法电子邮件验证 安装 将此行添加到您的应用程序的Gemfile中: gem 'kickbox_rails' 然后执行: $ bundle ...
Rails插件为ActiveRecord模型提供了validates_url_format_of方法。 网址由regexp验证。 已知与Ruby 1.8和1.9.2兼容。 已知与ActiveRecord 3.0.0.beta,2.3.5、2.2.2、2.1.2兼容。 用法 安装插件后,它的用法像 ...
然后使用以下命令指定User记录中的哪个字段是电子邮件字段: class User < ApplicationRecord validates :email , presence : true , uniqueness : { case_sensitive : false } passwordless_with :email # &...
EmailValidator Rails 3+的电子邮件验证器。 使用RFC-3696验证支持符合RFC-2822和RFC-5321...安装添加到您的Gemfile中: gem 'email_validator' 跑步: bundle install用法将以下内容添加到您的模型中: validates :my
把宏风格的方法放在类别定义的前面(has_many, validates, 等等)。 偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证 # 使用 has_and_...
用法将 gem 添加到 Rails 项目的 Gemfile 中,然后 bundle install 开始。 gem 'mailgun_email_validator' 您需要在系统中添加一个MAILGUN_PUBLIC_KEY环境变量,例如在您的~/.bash_profile : export MAILGUN_PUBLIC...
Tset 为您的 Rails 应用程序中的模型生成测试。 为什么? TDD 很棒,但有时您只想编写一些代码。 输入“开发驱动测试”。 Tset 使您可以只编写代码并担心以后的测试。 安装 运行安装命令。 gem install tset ...
因此,您可以将这些匹配器与Rails,Sinatra或任何其他框架一起使用。 它的功能很完整。 匹配器假定您正在使用推荐的validation_helpers插件。 支持所有实例验证方法,即: validates_exact_length validates_...
Rspec :: RespectSelectorLimit 确保您CSS文件在旧版IE中不会损坏安装将此行添加到您的应用程序的Gemfile中: gem 'rspec-respect_selector_limit'用法 require 'rails_helper'it 'validates application.css' do ...
安装宝石文件: gem 'kawaii_validation'然后执行: $ bundle支持的版本Ruby 2.0、2.1、2.2、2.3、2.4和2.5(主干) Rails 3.2.x,4.0、4.1、4.2、5.0、5.1和5.2(边缘)用法捆绑了该gem后, validates方法将采用...
这个宝石的目的是为正在使用Rails 3+解决典型意大利业务问题的开发人员提供便利。 安装 将此行添加到您的应用程序的Gemfile中: gem 'italian_job' , '~> 1.0' 然后执行: $ bundle 用法 class MyClass < ...
开始使用 将此行添加到您的应用程序的Gemfile中: gem 'postjoy' 查询邮政编码 Postjoy . find ( 94109 ) # => { city : "San Francisco" , state : "California" , state_code : "CA" , latitude : 37.7917...