记录学习rails的过程。应用来自《web敏捷开发之道——应用rails进行敏捷web开发》
1. 创建app
rails new test-depot --skip-bundle
cd test-depot
bundle install --local
修改GemFile,添加gem 'thin'
2.创建数据库表product
./script/rails generate scaffold product title:string description:string image_url:string
invoke active_record create db/migrate/20121210082608_add_column.rb
class AddColumn < ActiveRecord::Migration def up add_column :products,:price,:decimal,:precision=>8,:scale=>2,:default=>0 end def down remove_column :products,:price end end
== AddColumn: migrating ====================================================== -- add_column(:products, :price, :decimal, {:default=>0, :precision=>8, :scale=>2}) -> 0.0051s == AddColumn: migrated (0.0052s) =============================================
class Product < ActiveRecord::Base attr_accessible :description, :image_url, :title,:price validates_presence_of :title,:description,:image_url # validates_numericality_of :price validates_uniqueness_of :title validates_length_of :title,:minimum => 10 validate :price_must_be_at_least_a_cent validates_format_of :image_url,:with=>%r{\.(gif|png|jpg)$}i,:message=>"must be a URL ForGIF,JPG or PNG image" def price_must_be_at_least_a_cent errors.add(:price,"should be at least 0.01") if price.nil?||price < 0.01 end end