2016-02-13 66 views
1

我是RoR的新手。我正在制作一个包含文章标题和文章文章的文章表格。我在桌上展示它。到现在为止还挺好。接下来我要做的是在我的表格中显示行号。 这里是我的代码:显示表中的每一行数RoR

<table> 
    <tr> 
here-> <th> RowNum </th> 
    <th>Title</th> 
    <th>Text</th> 
</tr> 

<% @articles.each do |article| %> 
<tr> 
    <td> <%= article.title %></td> 
    <td> <%= article.text %></td> 
</tr> 
<% end %> 
</table> 

我试着$。 magich变量和$ INPUT_LINE_NUMBER不起作用。

回答

4

使用@articles.each_with_index do |article, index|

然后在你的表中添加<%= index %>

+0

如何取出索引值? –

+0

我编辑了我的回复。 <%= index %> –

+0

非常感谢。有没有一种方法可以从1开始计数?因为它在第一行显示我0? –