我编写了以下 HAML 来代替脚手架索引页面:
%h1 Listing Races
%table
%tr
%th Name
%th Date
%th Details
~@races.each do |race|
%tr
%td= race.name
%td= race.date
%td= race.details
%td= link_to 'Show', race
%td= link_to 'Edit', edit_race_path(race)
%td= link_to 'Destroy', race, :confirm => 'Are you sure?', :method => :delete
%br
= link_to 'New Race', new_race_path
当页面被渲染时,表格会按预期打印出来,但之后,数组@races 也会被打印出来;例如:
[#<Race id: 1, name: "TestRace15", date: "2011-03-11 11:00:00", details: "Test Race to make sure that everything seems to wor...", created_at: "2011-03-03 00:16:09", updated_at: "2011-03-03 00:16:09">]
我是否对 HAML 中的循环结构做了错误的处理,或者什么会导致数组被渲染?
最佳答案
波浪号 ( ~
) 输出该行的结果,它是一个数组,因为 Array#each
返回原始数组。 =
和 ~
在这个意义上的作用类似;然而, ~
保留了 =
通常去除的空白。
您可能打算使用破折号 ( -
),它运行代码但不输出表达式的结果。
See the HAML docs for more :)
关于HAML 在 #each 之后呈现数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181636/