I am new in Rails and I am trying to put countries in array and then display them in a select box. My array looks like this:
country = {}
country['FR'] = 'France'
country['UK'] = 'United Kingdom'
Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your view, do the following :
The
@countries.to_awill transform your hash into a array.If your hash is the following :
After, after
to_ayou’ll have the following array :The
options_for_selectmethod takes an array and create the options tags.When the entry of the array is an array itself, the first entry (France or United Kingdom here) is the value. And the second one is the key (FR or UK here).
The
select_tagfunction creates a select tag.