2012-04-02 48 views
1

我有兩個模型(問題和解答)稍微跟隨流行Railscasts不需要(元?)中的Ruby/Rails數據

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爲:

= 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;,] 

如何刪除這個有什麼想法?我找不到這些額外數據的存在。

非常感謝提前!

回答

3

更換

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

隨着

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

(第一版打印出@ question.answers的內容,第二隻運行循環)

插入見haml documentation紅寶石vs運行紅寶石

+0

是的,就是這樣。感謝你的回答! – mo5470 2012-04-02 10:23:44