Ruby on Rails列表页面如何进行优化
创始人
2024-06-16 06:30:19
0

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. <%=h product.send(column.name) %> 
  12. <% end %> 
  13. <%= link_to 'Show', :action => 'show',
     :id => product %> 
  14. <%= link_to 'Edit', :action => 'edit', 
    :id => product %> 
  15. <%= link_to 'Destroy', { :action => 
    'destroy', :id => product }, :confirm => 
    'Are you sure?', :method => :post %> 
  16.  
  17. <% end %> 
  18.  
  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.  /> 
  22. <%= link_to 'New product', :action => 'new' %> 

 

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

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

 

  1. Product Listing

     
  2.  
  3. <%  
  4. odd_or_even = 0 
  5. for product in @products  
  6. odd_or_even = 1 - odd_or_even  
  7. %> 
  8. ListLine<%= odd_or_even %>"> 
  9.  
  10. <%= product.image_url %>"/> 
  11.  
  12.  
  13. <%=
     h(product.title) %> 
  14. <%= h(truncate(product.description, 80)) %> 
  15.  
  16.  
  17. <%= product.date_available.strftime
    ("%y-%m-%d") %>
     
  18. $<%= sprintf("%0.2f", 
    product.price) %>
     
  19.  
  20.  
  21. <%= link_to 'Show', :action => 'show', 
    :id => product %>
     
  22. <%= link_to 'Edit', :action => 'edit', 
    :id => product %>
     
  23. <%= link_to 'Destroy', { :action => 
    'destroy', :id => product },  
  24. :confirm => "Are you sure?" %> 
  25.  
  26.  
  27. <% end %> 
  28.  
  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.  
  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列表页面的优化内容就介绍到这里。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...