2013-07-25 27 views
0

我有一組附件,註釋和附件數組。例如在包含陣列的導軌中呈現異構對象列表

@heteroCollection = [Attachment, Note, Note, [Attachment, Attachment, Attachment]] 

在我看來,我試圖使這個集合

<%= render :partial => @heteroCollection %> 

但軌道嗆說附件的陣列需要實現:to_partial_path

[Attachment, ..., Attachment] is not an ActiveModel-compatible object. It must implement :to_partial_path 

如果我刪除陣列對於相應的記錄,渲染效果很好,呼叫_attachment.html.erb_note.html.erb

有沒有一種方法來實現:to_partial_path數組沒有猴子修補?還是有另一個更好的解決方案?

回答

1

您可以創建類AttachmentArray,從Array擴展它並將to_partial_path方法添加到它。比把你的附件推到該類的新實例..

+0

好主意!我會給它一個鏡頭。 – grossadamm

+0

它的工作原理,謝謝...我會標記它回答,當它讓我。 – grossadamm

+0

太棒了!沒問題! –