简体   繁体   English

基本每个循环Ruby on Rails

[英]Basic Each Loop Ruby on Rails

I have created an app to experiment with HAML and Nitrous.io but seem to be having a basic issue with an each loop. 我已经创建了一个应用程序来试验HAML和Nitrous.io,但似乎每个循环都有一个基本问题。 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输出是:

<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? 我不确定这是否是一个linous.io运行Linux设置这是一个linus的事情? If anyone knows how I can get rid of this hash at the end that would be great! 如果有人知道如何在最后摆脱这个哈希会很棒!

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. 如果用短划线替换它,它只会评估你想要的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM