appointment: this demo ran in ubuntu, and Rails 2.2.2
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command
----------------------------------
Catalog Display
-seeing what the application looks like from the buyer's point view
----------------------------------
step 1
create the catalog listing
create a controller
script>>script/generate controller store index
and point the browser as http://localhost:3000/store
[if you see no route.... restart you server is ok]
and will add some thing for the store index
in controller: app/controllers/store_controller.rb
add index method to response the index request
def index
@products = Product.find_products_for_sale #1
end
#1 add find_products_for_sale method in model Product
in app/models/product.rb
def self.find_products_for_sale
find(:all, :order => "title")
end
edit view: app/views/store/index.html.erb
<% for product in @products -%>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%=h product.title %></h3>
<%= product.description %>
<div class="price-line">
<span class="price"><%= product.price %></span>
</div>
</div>
<% end %>
step2
add a page layout
views/layouts/store.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html>
<head>
<title>Pragprog Books Online Store</title>
<%= stylesheet_link_tag "depot" , :media => "all" %>
</head>
<body id="store">
<div id="banner">
<%= image_tag("logo.png" ) %>
<%= @page_title || "Pragmatic Bookshelf" %>
</div>
<div id="columns">
<div id="side">
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
<a href="http://www..../contact">Contact</a><br />
</div>
<div id="main">
<%= yield :layout %>
</div>
</div>
</body>
</html>
step3
format the price with the help method
edit view: app/views/store/index.html.erb
<span class="price" ><%= number_to_currency(product.price) %></span>
step4
add to cart
edit view: app/views/store/index.html.erb
<%= button_to "Add to Cart" %>