gpt4 book ai didi

ruby-on-rails - 基本的每个循环 Ruby on Rails

转载 作者:行者123 更新时间:2023-12-04 06:35:33 26 4
gpt4 key购买 nike

我创建了一个应用程序来试验 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">]

在我的索引 Controller 中,我有:
class ListsController < ApplicationController
def index
@list = List.all
end
end

在我的索引 View 中,我有:
%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 设置?如果有人知道我最后如何摆脱这个哈希,那就太好了!

最佳答案

你的错误在这一行:

= @list.each do |list|

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

关于ruby-on-rails - 基本的每个循环 Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18611721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com