为什么我的 erb 代码会产生时间戳,而教程不会?

Why does my erb code result in timestamps when tutorial doesn't?

我正在 http://guides.rubyonrails.org/getting_started.html 阅读 Rails 入门指南,我已完成 5.8 的最后一部分。我的输出包含这个额外的行,我无法弄清楚它来自哪里。有人可以帮我吗?

[#<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>

删除 =

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

应该是

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

只需将 <%= @articles.each do |article| %> 替换为 <% @articles.each do |article| %>

说明: 在 Ruby 中,您不必指定 return 值。每个方法的最后一条语句将自动 returned。因此 @articles.each 将 return 完整数组。 erb 标签 <%= 只是打印出来。