Ruby / Rails中不需要的(meta?)数据

时间:2012-04-02 08:57:50

标签: ruby-on-rails ruby haml nested-forms

我有两个模型(问题和答案)略微遵循流行的Railscasts

class Question < ActiveRecord::Base 
  has_many :answers
  accepts_nested_attributes_for :answers, :allow_destroy => true
end

class Answer < ActiveRecord::Base
  belongs_to :question
  attr_accessible :content
end

在我的question_controller.rb的编辑方法中构建答案(出于我的应用目的):

2.times do
    @question.answers.build
end

并在编辑视图中呈现 - edit.html.haml as:

= f.fields_for :answers do |builder|
    = render "answer_fields", :f => builder

当我想要显示答案时,会出现问题。在show.html.haml里面,我这样做:

= for answer in @question.answers
  = answer.content

显示了答案内容,但我也得到了这个(不需要的)HTML代码:

  [#&lt;Answer id: 11, question_id: 22, content: &quot;100&quot;, created_at: &quot;2012-04-02 08:34:50&quot;, updated_at: &quot;2012-04-02 08:34:50&quot;&gt;,]

有关如何删除此内容的任何想法?我找不到存在这些额外数据的任何理由。

非常感谢提前!

1 个答案:

答案 0 :(得分:3)

替换

= for answer in @question.answers
  = answer.content

使用

- for answer in @question.answers
  = answer.content

(第一个版本打印出@ question.answers的内容,第二个版本只运行循环)

请参阅haml documentation以插入ruby与正在运行的ruby