创建项目,使用mysql数据库作为数据源
rails --database=mysql cart
设定Rails DB Environment
rake db:create RAILS_ENV="development"
运行Web服务器
ruby script/server
Product产品的制造
使用scaffold创建product(title,description,image_url)
ruby script/generate scaffold product title:string description:text image_url:string rake db:migrate
添加migration(add_price_to_product) 添加price(decimal)字段
ruby script/generate migration add_price_to_product price:decimal rake db:migrate
添加Product 测试数据
ruby script/generate migration add_test_data
class AddTestData < ActiveRecord::Migration def self.up Product.delete_all Product.create( :title => 'Progmatic Version Control', :description => %{This book is a recipe-based approach to using Subversion that will get you up and running quickly
}, :image_url => '/images/svn.jpg', :price => 28.50) end def self.down Product.delete_all end end
为model(Product)添加Validaton
class Product < ActiveRecord::Base validates_presence_of :title, :description, :image_url validates_numericality_of :price validate :price_must_be_at_least_a_cent validates_uniqueness_of :title validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i, :message => "must be a URL for GIF/JPG/PNG" has_many :line_items protected def price_must_be_at_least_a_cent errors.add(:price,"should be at least 0.01") if price.nil? || price < 0.01 end def self.find_products_for_sale find(:all , :order => "title") end end
rake db:migrate
用rake生成的ProductControl,包含index、show、new、edit、create、update等方法,基本的CRUD操作都已具备
class ProductsController < ApplicationController # GET /products # GET /products.xml def index @products = Product.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @products } end end # GET /products/1 # GET /products/1.xml def show @product = Product.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @product } end end # GET /products/new # GET /products/new.xml def new @product = Product.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @product } end end # GET /products/1/edit def edit @product = Product.find(params[:id]) end # POST /products # POST /products.xml def create @product = Product.new(params[:product]) respond_to do |format| if @product.save format.html { redirect_to(@product, :notice => 'Product was successfully created.') } format.xml { render :xml => @product, :status => :created, :location => @product } else format.html { render :action => "new" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end # PUT /products/1 # PUT /products/1.xml def update @product = Product.find(params[:id]) respond_to do |format| if @product.update_attributes(params[:product]) format.html { redirect_to(@product, :notice => 'Product was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end # DELETE /products/1 # DELETE /products/1.xml def destroy @product = Product.find(params[:id]) @product.destroy respond_to do |format| format.html { redirect_to(products_url) } format.xml { head :ok } end end end
用于CRUD操作Product的页面也由rake动态生成,代码如下:
index.html.erb
Listing products
Title | Description | Image url | |||
---|---|---|---|---|---|
<%=h product.title %> | <%=h product.description %> | <%=h product.image_url %> | <%= link_to 'Show', product %> | <%= link_to 'Edit', edit_product_path(product) %> | <%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to 'New product', new_product_path %>
new.html.erb
New product
<% form_for(@product) do |f| %> <%= f.error_messages %><%= f.label :title %>
<%= f.text_field :title %><%= f.label :description %>
<%= f.text_area :description %><%= f.label :image_url %>
<%= f.text_field :image_url %><%= f.submit 'Create' %>
<% end %> <%= link_to 'Back', products_path %>
edit.html.erb
Editing product
<% form_for(@product) do |f| %> <%= f.error_messages %><%= f.label :title %>
<%= f.text_field :title %><%= f.label :description %>
<%= f.text_area :description %><%= f.label :image_url %>
<%= f.text_field :image_url %><%= f.submit 'Update' %>
<% end %> <%= link_to 'Show', @product %> | <%= link_to 'Back', products_path %>
show.html.erb
Title: <%=h @product.title %>
Description: <%=h @product.description %>
Image url: <%=h @product.image_url %>
<%= link_to 'Edit', edit_product_path(@product) %> | <%= link_to 'Back', products_path %>