为什么rails会将数据转储到每个页面上

时间:2012-03-26 22:21:55

标签: ruby-on-rails ruby

我有一个名为list_controller的简单控制器,其索引基本上是

def index
    @lists = List.all
end

然后我有一个名为index.html.haml的视图,它看起来像:

%h1 My lists
= @lists.each do |list|
    .row-fluid
        %h2= list.title
        %p= list.description
%hr
= link_to "New list", new_list_url

这可以工作和渲染,但在我的列表的底部是列表对象的红宝石打印,在这种情况下是一个很大的丑陋:

[#<List id: 1, title: "Petes todo list", description: "This is petes main list, all sorts of good stuff", created_at: "2012-03-26 21:42:57", updated_at: "2012-03-26 21:42:57">, #<List id: 2, title: "Petes work list", description: "A list for petes work stuff", created_at: "2012-03-26 22:09:50", updated_at: "2012-03-26 22:09:50">]

为什么会这样?我怎么能阻止它?

2 个答案:

答案 0 :(得分:3)

您正在输出each的结果,更改:

= @lists.each do |list|

为:

- @lists.each do |list|

Haml文档:= vs -

答案 1 :(得分:1)

= @lists.each do |list|告诉Haml向右评估Ruby代码,然后打印出返回值。您应该将视图重写为

%h1 My lists
- @lists.each do |list|
    .row-fluid
        %h2= list.title
        %p= list.description
%hr
= link_to "New list", new_list_url