Amitoj Singh
Amitoj Singh

Reputation: 131

Using 'collection_select' with an array

I am trying to display an array in drop down list using collection_select. My array is:

"[\"\", \"Jacket\", \"Shirt\"]"

The code is below:

<%= form.collection_select(:parts, @parts, :to_s, include_blank: false,prompt:"Select the part" )%>

I get an error:

ActionView::Template::Error (undefined method `map' for "[\"\", \"jacket\", \"shirt\"]":String
Did you mean?  tap):

Upvotes: 1

Views: 299

Answers (1)

Corba
Corba

Reputation: 309

The problem is your "array" isn't an array, but a JSON string.

If you parse the json string to an array this should work.

parsed = JSON.parse("[\"\", \"Jacket\", \"Shirt\"]")

Upvotes: 5

Related Questions