rails学习笔记:维护商品信息任务B
2015-09-15 23:30
639 查看
修改数据模型
/app/models/product.rb
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)\z}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
这段代码用来检查以下几项内容
字段的标题、描述以及图像的URL不是空的
价格是一个有效的数字,且不得少于$0.01
标题在所在商品中是唯一的
图像的URL看起来是有效的
/app/models/product.rb
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)\z}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
这段代码用来检查以下几项内容
字段的标题、描述以及图像的URL不是空的
价格是一个有效的数字,且不得少于$0.01
标题在所在商品中是唯一的
图像的URL看起来是有效的
相关文章推荐
- 2015第37周二foxmail邮箱客户端迁移
- raise KeyError("Spider not found: {}".format(spider_name)) KeyError: 'Spider not found: novelspider'
- Aizu 2538 Stack Maze【记忆化搜索】
- Adb fail to restart! ADB server didn't ACK
- 【STL】STL之pair
- 怎么理解SpriteKit中的waitForDuration:withRange:方法
- Corsair DOMINATOR Platinum 海盗船3300MHz C16 DDR4 4GB*4 内存条 $336.77 国行售价¥5000+
- [Leetcode]Paint House II
- grails 事务 Transactional
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"
- sqrt和Hailstone
- sleep、yield、wait知识点
- mmap例子详解 http://blog.csdn.net/eroswang/article/details/1908842
- Number of Airplanes in the Sky
- MongoDB性能篇 -创建索引,组合索引,唯一索引,删除索引和explain执行计划
- Light oj 1071 - Baker Vai(记忆化)
- codeforces #576E Painting Edges 分治+并查集
- failed to update auto layout status: the agent crashed within Xcode7
- API介绍:iBeacons、Sprite Kit、Game Center以及AirDrop等
- 用<%# DataBinder.Eval(Container.DataItem,"ID")%>显示数据的,标题太长规定字数,多余的用"..."