agile rails depot 2

本文介绍如何使用Ruby on Rails 2.2.2搭建一个简单的在线商店应用,包括创建控制器、模型及视图,实现商品展示功能,并添加购物车按钮。
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
 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 %>


[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" %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值