Ruby on Rails列表页面如何进行优化

开发 开发工具
Ruby on Rails列表页面在实际编程中是非常常见的。对于初学者来说,对于列表的优化是一个比较重要的知识点。希望大家可以通过本文介绍的内容充分掌握这一技巧。

Ruby on Rails列表页面可以被我们进行优化。美化后的列表页面可以帮助我们一目了然的读懂列表,方便我们编写程序。在这里我们将会介绍Ruby on Rails列表页面的一些美化技巧。#t#

1.  打开appviewsadminlist.rhtml文件,可以看到下面的代码

 

  1. < h1>Listing products< /h1> 
  2. < table> 
  3. < tr> 
  4. <% for column in Product.content_columns %> 
  5. < th>< %= column.human_name %>< /th> 
  6. <% end %> 
  7. < /tr> 
  8. <% for product in @products %> 
  9. < tr> 
  10. <% for column in Product.content_columns %> 
  11. <td><%=h product.send(column.name) %></td> 
  12. <% end %> 
  13. <td><%= link_to 'Show', :action => 'show',
     
    :id => product %></td> 
  14. <td><%= link_to 'Edit', :action => 'edit', 
    :id => product %></td> 
  15. <td><%= link_to 'Destroy', { :action => 
    'destroy', 
    :id => product }, :confirm => 
    'Are you sure?', 
    :method => :post %></td> 
  16. </tr> 
  17. <% end %> 
  18. </table> 
  19. <%= link_to 'Previous page', { :page =>
     @product_pages.current.previous } if 
    @product_pages.current.previous %
    > 
  20. <%= link_to 'Next page', { :page => 
    @product_pages.current.next } if 
    @product_pages.current.next %
    > 
  21. <br /> 
  22. <%= link_to 'New product', :action => 'new' %> 

 

可以看到,list页面实际上是对Products做循环,然后对每行,每列逐个输出到一个Table中,而link_to函数,我们在前面的内容中也使用过。

Ruby on Rails列表页面2.修改appviewsadminlist.rhtml的内容,如下:

 

  1. <h1>Product Listing</h1> 
  2. <table cellpadding="5" cellspacing="0"> 
  3. <%  
  4. odd_or_even = 0 
  5. for product in @products  
  6. odd_or_even = 1 - odd_or_even  
  7. %> 
  8. <tr valign="top" class="
    ListLine<%= odd_or_even %>"
    > 
  9. <td> 
  10. <img width="60" height="70" src="
    <%= product.image_url %>"
    /> 
  11. </td> 
  12. <td width="60%"> 
  13. <span class="ListTitle"><%=
     h(product.title) %
    ></span><br /> 
  14. <%= h(truncate(product.description, 80)) %> 
  15. </td> 
  16. <td align="right"> 
  17. <%= product.date_available.strftime
    ("%y-%m-%d") %
    ><br/> 
  18. <strong>$<%= sprintf("%0.2f", 
    product.price) %
    ></strong> 
  19. </td> 
  20. <td class="ListActions"> 
  21. <%= link_to 'Show', :action => 'show', 
    :id => product %><br/> 
  22. <%= link_to 'Edit', :action => 'edit', 
    :id => product %><br/> 
  23. <%= link_to 'Destroy', { :action => 
    'destroy', 
    :id => product },  
  24. :confirm => "Are you sure?" %> 
  25. </td> 
  26. </tr> 
  27. <% end %> 
  28. </table> 
  29. <%= if @product_pages.current.previous  
  30. link_to("Previous page", { :page => 
    @product_pages.current.previous })  
  31. end  
  32. %> 
  33. <%= if @product_pages.current.next  
  34. link_to("Next page", { :page =>
     @product_pages.current.next })  
  35. end  
  36. %> 
  37. <br /> 
  38. <%= link_to 'New product', :action => 'new' %> 

 


Ruby on Rails列表页面3.在上面的代码里,我们可以看到td class="ListActions"这样的代码,下来我们添加这些css样式的内容:

将下面的内容添加到publicstylesheets scaffold.css文件中:

 

  1. .ListTitle {  
  2. color: #244;  
  3. font-weight: bold;  
  4. font-size: larger;  
  5. }  
  6. .ListActions {  
  7. font-size: x-small;  
  8. text-align: right;  
  9. padding-left: 1em;  
  10. }  
  11. .ListLine0 {  
  12. background: #e0f8f8;  
  13. }  
  14. .ListLine1 {  
  15. background: #f8b0f8;  

OK,今天Ruby on Rails列表页面的优化内容就介绍到这里。

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

2009-12-16 15:23:33

Ruby on rai

2009-12-16 17:24:26

Ruby on Rai

2010-07-12 09:22:05

RubyRuby on rai

2017-07-28 11:31:59

iOS结构优化项目

2015-10-14 17:27:18

性能

2015-10-10 11:00:05

RubyRails性能

2010-03-15 15:11:50

Python列表

2009-08-27 10:21:22

Ruby on Rai

2009-12-15 10:31:30

Ruby rails页

2009-12-17 09:14:14

Ruby on Rai

2009-08-06 09:13:36

Ruby on Rai

2009-12-14 15:37:35

Ruby on Rai

2020-02-12 08:50:05

Linux命令启动时间

2011-07-06 08:46:30

2010-07-16 14:30:25

Perl数组

2009-12-16 17:00:43

Ruby on Rai

2009-12-14 15:30:43

安装Ruby on R

2009-12-17 14:29:50

Ruby on Rai

2024-02-22 10:27:00

Python开发

2023-12-01 10:19:00

接口优化事务
点赞
收藏

51CTO技术栈公众号