The Depot Application

The Depot Application

6.4 Iteration A3: Validate! 6.2 Creating the Products Model and Maintenance Application

6.3 Iteration A2: Add a Missing Column

rails generate migration add_price_to_product price:decimal
      invoke  active_record
      create    db/migrate/20120629191114_add_price_to_product.rb
cat db/migrate/20120629191114_add_price_to_product.rb
class AddPriceToProduct < ActiveRecord::Migration
  def self.up
    add_column :products, :price, :decimal
  end
 
  def self.down
    remove_column :products, :price
  end
end
edit db/migrate/20120629191114_add_price_to_product.rb
class AddPriceToProduct < ActiveRecord::Migration
  def self.up
    add_column :products, :price, :decimal,
      :precision => 8, :scale => 2, :default => 0
  end
 
  def self.down
    remove_column :products, :price
  end
end
rake db:migrate
mv 20120629191114_add_price_to_product.rb 20110711000002_add_price_to_product.rb
==  AddPriceToProduct: migrating ==============================================
-- add_column(:products, :price, :decimal, {:precision=>8, :scale=>2, :default=>0})
   -> 0.0074s
==  AddPriceToProduct: migrated (0.0075s) =====================================
 
sqlite3 db/development.sqlite3 .schema
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "image_url" varchar(255), "created_at" datetime, "updated_at" datetime, "price" decimal(8,2) DEFAULT 0);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
edit app/views/products/index.html.erb
<h1>Listing products</h1>
 
<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
    <th>Price</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
 
<% @products.each do |product| %>
  <tr>
    <td><%= product.title %></td>
    <td><%= product.description %></td>
    <td><%= product.image_url %></td>
    <td><%= product.price %></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 %>
edit app/views/products/_form.html.erb
<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
 
      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description, :rows => 6 %>
  </div>
  <div class="field">
    <%= f.label :image_url %><br />
    <%= f.text_field :image_url %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
edit app/views/products/show.html.erb
<p id="notice"><%= notice %></p>
 
<p>
  <b>Title:</b>
  <%= @product.title %>
</p>
 
<p>
  <b>Description:</b>
  <%= @product.description %>
</p>
 
<p>
  <b>Image url:</b>
  <%= @product.image_url %>
</p>
 
<p>
  <b>Price:</b>
  <%= @product.price %>
</p>
 
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
get /products

Listing products

Title Description Image url Price
Pragmatic Version Control <p> This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. dont use any version control at all. Many others dont use it well, and end up experiencing time-consuming problems. </p> /images/svn.jpg 0.0 Show Edit Destroy

New Product
get /products/1

Title: Pragmatic Version Control

Description: <p> This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. dont use any version control at all. Many others dont use it well, and end up experiencing time-consuming problems. </p>

Image url: /images/svn.jpg

Price: 0.0

Edit | Back
get /products/new

New product





Back
rake test
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
 
 
Finished in 0.118637 seconds.
 
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
 
8.43 tests/s, 8.43 assertions/s
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
 
 
Finished in 0.47383 seconds.
 
7 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
 
14.77 tests/s, 21.10 assertions/s
edit app/views/products/show.html.erb
<p id="notice"><%= notice %></p>
 
<p>
  <b>Title:</b>
  <%= @product.title %>
</p>
 
<p>
  <b>Description:</b>
  <%= @product.description %>
</p>
 
<p>
  <b>Image url:</b>
  <%= @product.image_url %>
</p>
 
<p>
  <b>Price:</b>
  <%= @product.price %>
</p>
 
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
get /products/1

Title: Pragmatic Version Control

Description: <p> This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. dont use any version control at all. Many others dont use it well, and end up experiencing time-consuming problems. </p>

Image url: /images/svn.jpg

Price: 0.0

Edit | Back

6.4 Iteration A3: Validate! 6.2 Creating the Products Model and Maintenance Application