Created
July 7, 2011 20:00
-
-
Save viola/1070410 to your computer and use it in GitHub Desktop.
Example of fetching Hash (key,value) to the simple_form collection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- model | |
some sort of constant hash: | |
HASH_NAME = { | |
0 => "Choose:", | |
1 => "On-Campus Recruiting - CSO",· | |
2 => "CSO Staff Referral", | |
3 => "Faculty Contact",· | |
4 => "Career Day",· | |
5 => "CSO Summer Job Listing",· | |
6 => "Alumni Contact",· | |
7 => "Personal Contact",· | |
8 => "Other"· | |
} | |
-- view | |
<%= f.input :some_field, :collection => Model::HASH_NAME.sort.map {|k,v| [v,k]} %> | |
This would output nice select with select-value as hash key and select-name as hash value, such as: | |
<select id="form_application_job_source" class="select required" name="form_application[job_source]"> | |
<option value="0">Choose:</option> | |
<option value="1">On-Campus Recruiting - CSO</option> | |
<option value="2">CSO Staff Referral</option> | |
<option value="3">Faculty Contact</option> | |
<option value="4">Career Day</option> | |
<option value="5">CSO Summer Job Listing</option> | |
<option value="6">Alumni Contact</option> | |
<option selected="selected" value="7">Personal Contact</option> | |
<option value="8">Other</option> | |
</select> |
Again. Thanks! Exactly what I needed today.
Thanks! - Helped me troubleshoot getting my MDB select working properly:
"
<%= options_for_select(Customer::PROVINCES.map {|k,v| [v,k]}) %>
Still a relevant and very useful thread, thanks all.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
6 years later! Thank you so much!