Basic Each Loop Ruby on Rails

Tom Pinchen picture Tom Pinchen · Sep 4, 2013 · Viewed 15.1k times · Source

I have created an app to experiment with HAML and Nitrous.io but seem to be having a basic issue with an each loop. My current setup it outputting the following below the correct content:

[#<List id: 1, name: "ToM5", telephone: 2357928154, created_at: "2013-09-03 14:58:22", updated_at: "2013-09-03 17:04:54">, #<List id: 2, name: "Tom9 ", telephone: 2345701247, created_at: "2013-09-03 14:59:40", updated_at: "2013-09-03 17:05:00">, #<List id: 3, name: "Test3", telephone: 235821421, created_at: "2013-09-03 15:27:11", updated_at: "2013-09-03 17:05:05">]

In my index controller I have:

class ListsController < ApplicationController
  def index
    @list = List.all
  end
end

In my index view I have:

%h2 Here they are:

= @list.each do |list|
  .row
    %span{:class => "id"}= list.id
    %span{:class => "name"}= list.name
    %span{:class => "telephone"}= list.telephone
    %span{:class => "actions"}= link_to "Edit", edit_list_path(list)

= link_to "New", new_list_path

The HTML Output I am getting is:

<html>
<head>
   ...  
</head>
<body style="">
  <h1 class="heading">This is Lists</h1>
  <div class="wrap">
     <h2>Here they are:</h2>
<div class="row">
  <span class="id">1</span>
  <span class="name">ToM5</span>
  <span class="telephone">2357928154</span>
  <span class="actions"><a href="/lists/1/edit">Edit</a></span>
</div>
<div class="row">
  <span class="id">2</span>
  <span class="name">Tom9</span>
  <span class="telephone">2345701247</span>
  <span class="actions"><a href="/lists/2/edit">Edit</a></span>
</div>
<div class="row">
  <span class="id">3</span>
  <span class="name">Test3</span>
  <span class="telephone">235821421</span>
  <span class="actions"><a href="/lists/3/edit">Edit</a></span>
</div>
[#&lt;List id: 1, name: "ToM5", telephone: 2357928154, created_at: "2013-09-03 14:58:22", updated_at: "2013-09-03 17:04:54"&gt;, #&lt;List id: 2, name: "Tom9 ", telephone: 2345701247, created_at: "2013-09-03 14:59:40", updated_at: "2013-09-03 17:05:00"&gt;, #&lt;List id: 3, name: "Test3", telephone: 235821421, created_at: "2013-09-03 15:27:11", updated_at: "2013-09-03 17:05:05"&gt;]
<a href="/lists/new">New</a>

  </div>


</body></html>

I am not sure if perhaps this is a linus thing as Nitrous.io runs a linux setup? If anyone knows how I can get rid of this hash at the end that would be great!

Answer

sevenseacat picture sevenseacat · Sep 4, 2013

Your error is in this line:

= @list.each do |list|

Equals is for evaluation and also printing the result (which in this case is the object you're iterating over). If you replace it with a dash, it will just evaluate the expression which is what you want.