collection_select和options_for_select不起作用

时间:2014-05-20 12:36:48

标签: ruby-on-rails-4

我正在尝试使用自定义数据创建一个collection_select选项。我知道如何使用自定义数据属性创建选项,但是当我尝试将这些选项添加到我的collection_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搜索并尝试了很多变化,但我希望有人可以帮助我形成这一点。

我知道我的代码不包含数据属性,但我简化了我的示例。

1 个答案:

答案 0 :(得分:3)

我不知道你是否已经解决了这个问题,但这应该有效:

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