2017-01-17 16 views
0

我正在开始使用Rails指南,网址为http://guides.rubyonrails.org/getting_started.html,我已经到了5.8的最后一部分。我的输出包含这个额外的行,我不知道它来自哪里。有人可以帮我吗?为什么我的erb代码在教程不会导致时间戳?

[#<Article id: 1, title: "Test", text: "tests", created_at: "2017-01-17 20:01:14", updated_at: "2017-01-17 20:01:14">] 

教程码 -

<h1>Listing articles</h1> 

<table> 
    <tr> 
    <th>Title</th> 
    <th>Text</th> 
    </tr> 

    <% @articles.each do |article| %> 
    <tr> 
     <td><%= article.title %></td> 
     <td><%= article.text %></td> 
     <td><%= link_to 'Show', article_path(article) %></td> 
    </tr> 
    <% end %> 
</table> 

我的代码是这样的 -

<div> 
<%= @articles.each do |article| %> 
    <div> 
     <div>Title:</div> 
     <div><%= article.title %></div> 
     <div>Text:</div> 
     <div><%= article.text %></div> 
     <div><%= link_to 'Show', article_path(article) %></div> 
    </div> 
<% end %> 
</div> 

回答

3

取出=

<%= @articles.each do |article| %> 

应该

<% @articles.each do |article| %> 
+0

谢谢,无法相信我错过了我发布的问题相比之前的10倍左右。作为一个利益问题,为什么<%=在这里的作用与<%不同? – SMC

+1

看看[这个SO问题](http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails)。 – mmichael

2

<% @articles.each do |article| %>

说明只需更换<%= @articles.each do |article| %>: 在Ruby中,你没有指定一个返回值。每个方法的最后一个语句将自动返回。因此,@articles.each将返回完整的数组。 erb标签<%=只是打印。

相关问题