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

风流意气都作罢 提交于 2019-12-13 06:10:13

问题


I'm doing the Getting Started with Rails guide at http://guides.rubyonrails.org/getting_started.html and I'm up to the last section of 5.8. My output contains this additional line and I can't figure out where it comes from. Can someone help me out?

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

Tutorial code -

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

My code is this -

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

回答1:


Remove the =

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

should be

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



回答2:


Just replace <%= @articles.each do |article| %> with <% @articles.each do |article| %>

Explanation: In Ruby, you do not have to specify a return value. The last statement of every method will be returned automatically. @articles.each will therefore return the complete array. The erb tag <%= just prints that.



来源:https://stackoverflow.com/questions/41706300/why-does-my-erb-code-result-in-timestamps-when-tutorial-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!