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
----------------------------------
[b]Catalog Display
-seeing what the application looks like from the buyer's point view[/b]
----------------------------------
[b]step 1[/b]
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
#1 add find_products_for_sale method in model Product
in app/models/product.rb
edit view: app/views/store/index.html.erb
[b]step2 [/b]
add a page layout
views/layouts/store.html.erb
[b]step3 [/b]
format the price with the help method
edit view: app/views/store/index.html.erb
[b]step4 [/b]
add to cart
edit view: app/views/store/index.html.erb
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command
----------------------------------
[b]Catalog Display
-seeing what the application looks like from the buyer's point view[/b]
----------------------------------
[b]step 1[/b]
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")
endedit 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 %>
[b]step2 [/b]
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>
[b]step3 [/b]
format the price with the help method
edit view: app/views/store/index.html.erb
<span class="price" ><%= number_to_currency(product.price) %></span>[b]step4 [/b]
add to cart
edit view: app/views/store/index.html.erb
<%= button_to "Add to Cart" %>
本文介绍如何使用Ruby on Rails 2.2.2搭建一个简单的在线商店应用,包括创建控制器、模型及视图,实现商品展示功能,并添加购物车按钮。
152

被折叠的 条评论
为什么被折叠?



