我创建了一个应用程序来试验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>
[#<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">]
<a href="/lists/new">New</a>
</div>
</body></html>
我不确定这是否是一个linous.io运行Linux设置的事情?如果有人知道如何在最后摆脱这个哈希会很棒!
答案 0 :(得分:9)
您的错误就在这一行:
= @list.each do |list|
等于用于评估并打印结果(在这种情况下是您要迭代的对象)。如果用短划线替换它,它只会评估你想要的表达式。