Ruby on Rails创建购物页面技巧一点通

开发 开发工具
Ruby on Rails创建购物页面是网页开发人员在实际操作中经常会遇到的问题。那么如何才能正确的实现这一功能需求呢?

Ruby on Rails是一款性能不错的WEB开发框架。许多编程人员都靠这个开发框架来实现简约编程。在这里我们将会为大家详细介绍有关Ruby on Rails创建购物页面的一些技巧。#t#

 

这次我们使用上面维护的Products列表来创建一个最终用户使用的购物页面。

1.创建控制器(Controller),命名为store,我们通过命令行来创建它:

depot> ruby script/generate controller Store index

打开...rails_appsdepotappcontrollers目录下的store_controller.rb文件,向其中添加Ruby on Rails创建购物页面的代码:

 

  1. def index  
  2. @products = 
    Product.salable_items  
  3. end 

当然,我们还需要给Product定义salable_items方法,打开rails_appsdepotappmodels目录下的product.rb文件,添加代码:

 

  1. def self.salable_items  
  2. find(:all,  
  3. :conditions => 
    "date_available 
    < = now()",  
  4. :order => "date_available desc")  
  5. end 

2.创建表示层,在rails_appsdepotappviewsstore目录下,创建一个index.rhtml文件,修改其内容如下:

 

  1. < html> 
  2. < head> 
  3. < title>Pragprog Books Online Store
    < /title> 
  4. < %= stylesheet_link_tag "depot",
     
    :media => "all" %> 
  5. < /head> 
  6. < body> 
  7. < div id="banner"> 
  8. < img src="http://images.
    cnblogs.com/logo.png"
    /> ||  
  9. < %= @page_title || "Pragmatic 
    Bookshelf" %
    > 
  10. < /div> 
  11. < div id="columns"> 
  12. < div id="side"> 
  13. < a href="http://www....">Home
    < /a>< br /> 
  14. < a href="http://www..../faq">
    Questions< /a>< br /> 
  15. < a href="http://www..../news">
    News< /a>< br /> 
  16. < a href="http://www..../contact">
    Contact< /a>< br /> 
  17. < /div> 
  18. < div id="main"> 
  19. < %= @content_for_layout %> 
  20. < % for product in @products -%> 
  21. < div class="catalogentry"> 
  22. < img src="< %= product.image_url %>"/> 
  23. < h3>< %= h(product.title) %>< /h3> 
  24. < %= product.description %> 
  25. < span class="catalogprice">< %= 
    sprintf("$%0.2f", product.price) %
    >< /span> 
  26. < %= link_to 'Add to Cart',  
  27. {:action => 'add_to_cart', :id => product },  
  28. :class => 'addtocart' %>< br/> 
  29. < /div> 
  30. < div class="separator">&nbsp;< /div> 
  31. < % end %> 
  32. < %= link_to "Show my cart", 
    :action => "display_cart" %> 
  33. < /div> 
  34. < /div> 
  35. < /body> 
  36. < /html> 

可以看到,在index.rhtml中,使用了css样式,css样式的文件名字叫depot

< %= stylesheet_link_tag "depot", :media => "all" %>

我们可以在rails_appsdepotpublicstylesheets目录下创建一个depot.css文件来定义我们的样式以完成Ruby on Rails创建购物页面的操作。

责任编辑:曹凯 来源: 新客网
相关推荐

2009-12-15 10:31:30

Ruby rails页

2009-12-15 14:27:30

Ruby存取器

2009-12-14 15:30:43

安装Ruby on R

2009-12-17 17:37:42

Ruby on Rai

2010-01-04 19:22:49

Silverlight

2009-12-16 17:24:26

Ruby on Rai

2009-12-18 13:13:59

Ruby on Rai

2009-12-18 14:19:45

Ruby on Rai

2009-07-20 09:12:54

Ruby on Rai

2009-12-16 15:46:41

Ruby on rai

2009-12-17 14:36:57

Ruby on Rai

2009-12-16 17:07:27

Ruby on Rai

2012-07-27 09:36:33

金山WPSWPS移动版

2009-08-27 10:21:22

Ruby on Rai

2009-12-16 17:50:58

Ruby on Rai

2009-12-24 17:10:42

WPF动画类

2009-11-09 13:56:15

WCF Stream对

2009-10-22 09:29:23

CLR是什么

2009-12-31 13:45:13

Silverlight

2009-11-03 09:09:19

VB.NET类
点赞
收藏

51CTO技术栈公众号