应用rails进行敏捷web开发学习日记--创建列表

创建controller并包含index这个action方法

ruby script/generate controller store index

 

修改app\controllers\store_controller.rb

class StoreController < ApplicationController
  def index
    @products 
= Product.find(:all,:order=>"title")
  end

  def add_to_cart

    @products = Product.find(params[:id])

  end

end

 


 

 在app\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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Store: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'depot' %>
</head>
<body>
  
<div id="header">
    
<h1>Store</h1>
  </div>
  <div id="globalnav">
  
</div>
  <div id="tools">
    
<p id="notice"><%= flash[:notice] %></p>  
  </div>
  <div id="body">
    
<%= yield %>
  
</div>  
  <div id="footer">
    
<p class="center"><%=Time.now%></p>
  </div>
</body>
</html>

 修改app\views\store\index.html.erb

<h2>index</h2>

<% @products.each do |product| %>

<ul class="<%=cycle('list_odd','list_even')%>">

    <li class="image"> <img src="<%=product.image_url %>"/></li>

    <li class="id"><%=product.id%></li>

    <li class="title"><%=h(product.title).titleize %></li>

    <li class="description"><%=h(product.description) %></li>

    <li class="price"><%=number_to_currency(h(product.price)) %></li>

    <%=button_to"Add to cart",:action=>:add_to_cart,:id=>product%>

</ul>

<% end %>

 创建add_to_cart.html.erb


 <h2>add_to_cart</h2>

<ul>

    <li class="image"> <img src="\<%=@products.image_url %>"/></li>

    <li class="title"><%=h(@products.title).titleize %></li>

    <li class="price"><%=number_to_currency(h(@products.price)) %></li>

</ul>

<%= link_to 'Back To Store', :action=>"index" %>


访问http://127.0.0.1/store



posted @ 2009-05-15 11:57  Hi,Dan  阅读(376)  评论(0编辑  收藏  举报