2014-05-20 20 views
4

我想創建一個collection_select與選項上的自定義數據attibutes。我知道如何使用自定義數據屬性創建選項,但是當我嘗試將這些選項添加到我的collection_select時,我的代碼中斷了,無論我做什麼。collection_select和options_for_select不工作

下面的代碼工作

<%= f.collection_select :tag_ids, Tag.all, :id, :name, {}, {multiple: true} %> 

我然後修改它和它打破得到下述

<%= f.collection_select(:tag_ids, options_for_select(Tag.all.collect{|t| [t.name, t.id]}), {}, {multiple: true}) %> 

undefined method `map' for #<ActiveSupport::SafeBuffer:0x00000102df6648> 

錯誤我用Google搜索,並嘗試了許多變化,但我希望有人能幫助我形成這一點。

我知道我的代碼不包含數據屬性,但我簡化了我的示例。

回答

2

我不知道你是否已經解決了這個問題,但是這應該工作:

<%= f.select(:tag_ids, options_for_select(Tag.all.map{|t| [t.name, t.id]}), {}, {multiple: true}) %>