4

我创建了一个应用程序来试验 HAML 和 Nitrous.io,但似乎每个循环都有一个基本问题。我当前的设置它在正确的内容下方输出以下内容:

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

在我的索引控制器中,我有:

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

在我的索引视图中,我有:

%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

我得到的 HTML 输出是:

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

我不确定这是否是 linus 的事情,因为 Nitrous.io 运行 linux 设置?如果有人知道我如何在最后摆脱这个哈希,那就太好了!

4

1 回答 1

9

您的错误在这一行:

= @list.each do |list|

Equals 用于评估并打印结果(在这种情况下是您正在迭代的对象)。如果你用破折号替换它,它只会评估你想要的表达式。

于 2013-09-04T11:01:04.550 回答