1

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):
2
  • 2
    It's not an array, it's a string and that's why you get this error. Commented May 9, 2018 at 6:51
  • What is your question? Commented May 9, 2018 at 6:55

1 Answer 1

5

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\"]")
Sign up to request clarification or add additional context in comments.

1 Comment

@sawa That was a brainfart from my side, thanks for spotting that. Removed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.