Shubham
Shubham

Reputation: 13

How do I display a pipe-separated string in dropdown box?

I am getting a string seperated with pipe. I want to show it in dropdown box. How can i achieve that?Asume the data i am getting is in this format

0123~Apple|Banana|Grapes|Oranges

Upvotes: 0

Views: 93

Answers (1)

Nitin
Nitin

Reputation: 7366

Use this code to get array of string:

str = "0123~Apple|Banana|Grapes|Oranges"
@options = str.split('~')[1].split("|") #=> ["Apple", "Banana", "Grapes", "Oranges"] 

And to generate select tag use this:

<%= select_tag "xyz", options_for_select(@options) %>

Upvotes: 1

Related Questions