Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collection_select and options_for_select not working

I am trying to create a collection_select with custom data-attibutes on the options. I know how to create options with custom data attributes, but when I try to add those options to my collection_select my code breaks, not matter what I do.

The code below works

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

I then modify it to and it breaks giving the error below

<%= 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>

I have googled and tried many variations, but I was hoping someone could help me form this point.

I know my code does not include data-attributes, but I have simplified my example.

like image 954
Ryan-Neal Mes Avatar asked Oct 20 '22 08:10

Ryan-Neal Mes


1 Answers

I don't know if you already fixed this but this should work:

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

like image 158
Robin van Dijk Avatar answered Oct 23 '22 00:10

Robin van Dijk