`
wangtong40
  • 浏览: 253963 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Agile Web Development With Rails 3rd - Product

阅读更多

 

创建项目,使用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 =>
        %{<p>This book is a recipe-based approach to using Subversion that will get you up and running quickly</p>},
    :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

<h1>Listing products</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
  </tr>

<% @products.each do |product| %>
  <tr>
    <td><%=h product.title %></td>
    <td><%=h product.description %></td>
    <td><%=h product.image_url %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>

 

new.html.erb

<h1>New product</h1>

<% form_for(@product) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :image_url %><br />
    <%= f.text_field :image_url %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', products_path %>

 edit.html.erb

<h1>Editing product</h1>

<% form_for(@product) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :image_url %><br />
    <%= f.text_field :image_url %>
  </p>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>

 show.html.erb

<p>
  <b>Title:</b>
  <%=h @product.title %>
</p>

<p>
  <b>Description:</b>
  <%=h @product.description %>
</p>

<p>
  <b>Image url:</b>
  <%=h @product.image_url %>
</p>


<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
 
分享到:
评论

相关推荐

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    Agile+Web+Development+with+Rails中文版.pdf

    《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Agile Web Development with Rails 3nd Edition Beta.pdf

    ### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...

    Agile Web Development with Rails

    《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...

    agile web development with rails 5(英文电子书)

    agile web development with rails 5(英文电子书).............................................................................................................................................................

    Agile Web Development with Rails (PDF)

    《敏捷Web开发与Rails》是一本深度探讨如何利用Ruby on Rails框架进行敏捷Web开发的指导书籍,由Dave Thomas、David Heinemeier Hansson等多位在Rails社区有着深厚贡献的作者共同编写。本书不仅覆盖了Rails的基本...

    Agile Web Development with Rails-Second Edition-Beta一书例子

    《Agile Web Development with Rails-Second Edition-Beta》是一本专注于使用Ruby on Rails进行敏捷Web开发的书籍。这本书的第二版beta版提供了关于如何利用Rails框架高效构建动态、响应式网站的深入指导。作者们...

    Agile Web Development with Rails 4th -Final Edition

    《Agile Web Development with Rails 4th - Final Edition》是一本深入探讨Rails 3.0.5框架的权威指南,该书在2011年3月发布,为当时的技术环境提供了详尽的实践指导。Rails 3.0.5是Ruby on Rails框架的一个重要里程...

    Agile Web Development with Rails, 2nd Edition

    Justin Gehtland , Andreas Schwarz &lt;br&gt; &lt;br&gt;http://ecx.images-amazon.com/images/I/51YZKK7C2WL._SS500_.jpg&lt;br&gt;&lt;br&gt;&lt;br&gt;http://www.amazon.com/Agile-Web-Development-Rails-2nd/dp/0977616630

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    《敏捷Web开发与Rails:程序指南 第四版》是一本深度探讨使用Ruby on Rails框架进行敏捷Web应用开发的专业书籍。本书旨在帮助开发者充分利用Rails 4的特性,提高开发效率,实现快速迭代和高质量的代码编写。 Rails是...

    Agile Web Development with Rails,Fourth Edition 第四版

    《敏捷Web开发与Rails》第四版是一本专为软件开发者设计的权威指南,全面涵盖了使用Ruby on Rails框架进行敏捷Web应用开发的知识。Rails 3是该版本的重点,它引入了许多新特性和改进,使得开发过程更为高效且灵活。...

    Pragmatic - Agile Web Development with Rails

    《Pragmatic - Agile Web Development with Rails》是Ruby on Rails框架的经典教材,旨在引导初学者高效地学习敏捷Web开发。这本书的第三版于2009年发布,它结合了Pragmatic Programmers的实用主义理念与Ruby on ...

    Agile Web Development with Rails.3rd Edition.2009

    《敏捷Web开发与Rails》第三版是2009年发布的一本权威书籍,专注于使用Ruby on Rails框架进行高效、灵活的Web应用开发。这本书详细介绍了如何运用敏捷开发方法论来构建高质量的Web应用程序,特别关注Rails框架的实践...

Global site tag (gtag.js) - Google Analytics