6.4 Iteration A3: Validate! 6.2 Creating the Products Model and Maintenance Application
ruby script/generate migration add_price_to_product price:decimal
exists db/migrate
create db/migrate/20100528071110_add_price_to_product.rb
cat db/migrate/20100528071110_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/20100528071110_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 20100528071110_add_price_to_product.rb 20100301000002_add_price_to_product.rb
(in /home/rubys/git/awdwr/work-191-238/depot)
== AddPriceToProduct: migrating ==============================================
-- add_column(:products, :price, :decimal, {:precision=>8, :scale=>2, :default=>0})
-> 0.0011s
== AddPriceToProduct: migrated (0.0012s) =====================================
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>
</tr>
<% @products.each do |product| %>
<tr>
<td><%=h product.title %></td>
<td><%=h product.description %></td>
<td><%=h product.image_url %></td>
<td><%=h 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/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, :rows => 6 %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
edit app/views/products/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.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
edit app/views/products/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>
<p>
<b>Price:</b>
<%=h @product.price %>
</p>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
get /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 |
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 | Backget /products/new
rake test
/home/rubys/.rvm/rubies/ruby-1.9.1-p378/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.9.1-p378%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/products_helper_test.rb" "test/unit/product_test.rb"
(in /home/rubys/git/awdwr/work-191-238/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.1-p378%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.
Finished in 0.131338232 seconds.
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
/home/rubys/.rvm/rubies/ruby-1.9.1-p378/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.9.1-p378%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/products_controller_test.rb"
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.1-p378%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.......
Finished in 0.197580511 seconds.
7 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
/home/rubys/.rvm/rubies/ruby-1.9.1-p378/bin/ruby -I"lib:test" "/home/rubys/.rvm/gems/ruby-1.9.1-p378%global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
edit app/views/products/show.html.erb
<p>
<b>Title:</b>
<%=h @product.title %>
</p>
<p>
<b>Description:</b>
<%= @product.description %>
</p>
<p>
<b>Image url:</b>
<%=h @product.image_url %>
</p>
<p>
<b>Price:</b>
<%=h @product.price %>
</p>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
get /products/1
Title: Pragmatic Version Control
Description:
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.
Image url: /images/svn.jpg
Price: 0.0
Edit | Back6.4 Iteration A3: Validate! 6.2 Creating the Products Model and Maintenance Application