0

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>
LiamRyan
  • 1,892
  • 4
  • 32
  • 61

2 Answers2

3

Remove the =

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

should be

<% @articles.each do |article| %>
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • Thanks, can't believe I missed that I compared about 10 times before posting the question. As a matter of interest why does <%= act differently to <% here? – LiamRyan Jan 17 '17 at 20:29
  • 1
    Take a look at [this SO question](http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails). – vich Jan 17 '17 at 20:32
3

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.

Alex
  • 2,398
  • 1
  • 16
  • 30