Ruby on Rails列表页面可以被我们进行优化。美化后的列表页面可以帮助我们一目了然的读懂列表,方便我们编写程序。在这里我们将会介绍Ruby on Rails列表页面的一些美化技巧。#t#
1. 打开appviewsadminlist.rhtml文件,可以看到下面的代码
可以看到,list页面实际上是对Products做循环,然后对每行,每列逐个输出到一个Table中,而link_to函数,我们在前面的内容中也使用过。
Ruby on Rails列表页面2.修改appviewsadminlist.rhtml的内容,如下:
Product Listing
- <%
- odd_or_even = 0
- for product in @products
- odd_or_even = 1 - odd_or_even
- %>
ListLine<%= odd_or_even %>"> <%= product.image_url %>"/>
- <%=
h(product.title) %>
- <%= h(truncate(product.description, 80)) %>
- <%= product.date_available.strftime
("%y-%m-%d") %>
- $<%= sprintf("%0.2f",
product.price) %>- <%= link_to 'Show', :action => 'show',
:id => product %>
- <%= link_to 'Edit', :action => 'edit',
:id => product %>
- <%= link_to 'Destroy', { :action =>
'destroy', :id => product },- :confirm => "Are you sure?" %>
- <% end %>
- <%= if @product_pages.current.previous
- link_to("Previous page", { :page =>
@product_pages.current.previous })- end
- %>
- <%= if @product_pages.current.next
- link_to("Next page", { :page =>
@product_pages.current.next })- end
- %>
- <%= link_to 'New product', :action => 'new' %>
Ruby on Rails列表页面3.在上面的代码里,我们可以看到td class="ListActions"这样的代码,下来我们添加这些css样式的内容:
将下面的内容添加到publicstylesheets scaffold.css文件中:
- .ListTitle {
- color: #244;
- font-weight: bold;
- font-size: larger;
- }
- .ListActions {
- font-size: x-small;
- text-align: right;
- padding-left: 1em;
- }
- .ListLine0 {
- background: #e0f8f8;
- }
- .ListLine1 {
- background: #f8b0f8;
- }
OK,今天Ruby on Rails列表页面的优化内容就介绍到这里。