The Last Day Of Summer

.NET技术 C# ASP.net ActiveReport SICP 代码生成 报表应用 RDLC
posts - 306, comments - 2046, trackbacks - 78, articles - 3
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

在上一篇随笔中,我们给Products的创建页面添加了输入验证,今次的内容非常简单,来稍稍美化下Products的列表页面。

 

1.       打开app\views\admin\list.rhtml文件,可以看到下面的代码

<h1>Listing products</h1>

 

<table>

  <tr>

  <% for column in Product.content_columns %>

    <th><%= column.human_name %></th>

  <% end %>

  </tr>

 

<% for product in @products %>

  <tr>

  <% for column in Product.content_columns %>

    <td><%=h product.send(column.name) %></td>

  <% end %>

    <td><%= link_to 'Show', :action => 'show', :id => product %></td>

    <td><%= link_to 'Edit', :action => 'edit', :id => product %></td>

    <td><%= link_to 'Destroy', { :action => 'destroy', :id => product }, :confirm => 'Are you sure?', :method => :post %></td>

  </tr>

<% end %>

</table>

 

<%= link_to 'Previous page', { :page => @product_pages.current.previous } if @product_pages.current.previous %>

<%= link_to 'Next page', { :page => @product_pages.current.next } if @product_pages.current.next %>

 

<br />

 

<%= link_to 'New product', :action => 'new' %>

 

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

 

2.        修改app\views\admin\list.rhtml的内容,如下:

<h1>Product Listing</h1>

<table cellpadding="5" cellspacing="0">

  <%

  odd_or_even = 0

  for product in @products

  odd_or_even = 1 - odd_or_even

  %>

 

    <tr valign="top" class="ListLine<%= odd_or_even %>">

      <td>

        <img width="60" height="70" src="<%= product.image_url %>"/>

      </td>

      <td width="60%">

        <span class="ListTitle"><%= h(product.title) %></span><br />

      <%= h(truncate(product.description, 80)) %>

      </td>

      <td align="right">

        <%= product.date_available.strftime("%y-%m-%d") %><br/>

        <strong>$<%= sprintf("%0.2f", product.price) %></strong>

      </td>

      <td class="ListActions">

        <%= link_to 'Show', :action => 'show', :id => product %><br/>

        <%= link_to 'Edit', :action => 'edit', :id => product %><br/>

        <%= link_to 'Destroy', { :action => 'destroy', :id => product },

        :confirm => "Are you sure?" %>

      </td>

    </tr>

  <% end %>

</table>

<%= 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

%>

<br />

<%= link_to 'New product', :action => 'new' %>

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

将下面的内容添加到public\stylesheets\ 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;

}

4.        再次运行Productslist页面,可以看到效果,如图:

 

OK,今天的内容很简单,今后我会尽量多写些自己的东西,不光是抄书了。

Feedback

#1楼  回复 引用   

2008-01-15 16:11 by be0701[未注册用户]
请问 能把这个教程发给我吗?
邮箱:littlenewus@yahoo.com
小弟感激不尽。。

#2楼[楼主]  回复 引用 查看   

2008-01-15 16:15 by Cure      
@be0701
我也没有什么教程,只是按照书上一步步来的,看这里,有书的下载:
[置顶]Ruby on rails开发从头来 for .Neter 系列随笔(附ruby电子书下载)

#3楼  回复 引用   

2008-01-19 23:31 by 六翼[未注册用户]
不知道这句
------------------
<% for column in Product.content_columns %>

<td><%=h product.send(column.name) %></td>

<% end %>
------------------
里的那个“h”是什么意思呢?还是说“<%=h”是一起的?

#4楼  回复 引用   

2008-03-18 15:26 by cosmo[未注册用户]
你这个list rhtml 里的'Destroy' 写法不对,这样不能无法删除
正确的写法应该是
<%= link_to 'Destroy', { :action => 'destroy', :id => product },
:confirm => "Are you sure?",
:method => :post %>


其实我也是初学着发你又漏写的地方我给补上,防止其他初学着也遇到类似问题。o(∩_∩)o..

#5楼[楼主]  回复 引用 查看   

2008-03-19 15:11 by Cure      
@cosmo
非常感谢!!

#6楼  回复 引用   

2008-07-25 14:50 by 太平洋[未注册用户]
@六翼
h()是用来转换<%=...%>生成的字符串中有时会包含的"<"或"&"符号.
另外<%....-%>这里的"-"用来去掉换行

#7楼  回复 引用   

2009-09-04 14:43 by kitty liu[未注册用户]
好。对于我这样的初学者来说。真是太好了。

#8楼  回复 引用 查看   

2011-11-17 16:07 by 什么东东      
<img width="60" height="70" src="C:\Sample Pictures\Chrysanthemum.jpg"/>
这是网页源文件中显示的图片地址,图片是存在的,但页面中就是不显示,是怎么回事?