Building an application
word> rails new depot
depot> rails server
depot> rails generate scaffold Product title:string description:text image_url:string price:decimal
depot> rake db:migrate
<%= f.text_area :description, :rows => 6 %> #html text_area为6行
/depot/db/seeds.rb:
Product.delete_all
Product.create(:title => 'Web Design for Developers',
:description =>
%{
Web Design for Developers will show you how to make your
web-based application look professionally designed. We'll help you
learn how to pick the right colors and fonts, avoid costly interface
and accessibility mistakes -- your application will really come alive.
We'll also walk you through some common Photoshop and CSS techniques
and work through a web site redesign, taking a new design from concept
all the way to implementation.
},
:image_url => '/images/wd4d.jpg',
:price => 42.95)
depot> rake db:seed #添加数据到数据库
将图片放到该目录depot_b/public/images,用/images/1.jpg即可访问
将css文件放到depot/public/stylesheets/depot.css,视图文件即可调用。
Validation
加入models/product.rb:
validates :title, :description, :image_url, :presence => true # :presence => true 即不为空
validates :price, :numericality => {:greater_than_or_equal_to => 0.01} # 验证数字,并且大于等于0.01
validates :title, :uniqueness => true #唯一
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
#以什么结尾,加错误提示
阅读(328) | 评论(0) | 转发(0) |