Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42593
  • 博文数量: 25
  • 博客积分: 1420
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-14 22:16
文章分类

全部博文(25)

文章存档

2013年(5)

2010年(20)

我的朋友

分类: Web开发

2013-11-25 14:00:31

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) |
给主人留下些什么吧!~~