2016-02-13 30 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? –