Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of fetching Hash (key,value) to the simple_form collection.
-- 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>
@larryh

This comment has been minimized.

Copy link

commented Aug 23, 2011

Thanks for posting this, Viola - it saved me a bunch of time and worked like a charm!

@viola

This comment has been minimized.

Copy link
Owner Author

commented Aug 25, 2011

Glad to hear you found it useful!

@christianrojas

This comment has been minimized.

Copy link

commented Mar 5, 2012

Thanks for this post, very useful!

@viola

This comment has been minimized.

Copy link
Owner Author

commented Mar 23, 2012

Thx man! I'm happy to hear that some of my gists are useful.

@mkuklis

This comment has been minimized.

Copy link

commented May 1, 2012

Wiola I was just looking for this too and found this :) Thanks!

@viola

This comment has been minimized.

Copy link
Owner Author

commented May 1, 2012

awesome thx Michal!!

@gerwitz

This comment has been minimized.

Copy link

commented Jun 28, 2012

SImple code but it was still easier to google and find this than scratch it out. Thanks.

@viola

This comment has been minimized.

Copy link
Owner Author

commented Jun 29, 2012

yw!

@csgavino

This comment has been minimized.

Copy link

commented Dec 27, 2012

Saved me a lot of time too, thank you.

@ghost

This comment has been minimized.

Copy link

commented May 1, 2013

Awesome - great help for a newb. Thanks

@mahemoff

This comment has been minimized.

Copy link

commented May 20, 2013

Thanks for this. You can also use invert to do the transform:

<%= f.input :some_field, :collection => Model::HASH_NAME.sort.invert %>

@Libranner

This comment has been minimized.

Copy link

commented Aug 5, 2013

Thank you!

@eos87

This comment has been minimized.

Copy link

commented Nov 5, 2013

Thanks and +1 for @mahemoff solution

@rreynier

This comment has been minimized.

Copy link

commented Nov 19, 2013

@viola: Thanks for posting this, it is exactly what I was looking for!

@mahemoff: I am unable to get your invert method to work. I get the following error:

undefined method `invert' for [["abc", "ABC"], ["cde", "CDE"]]:Array

Here is my hash:

  KINDS = {
    'abc' => 'ABC',
    'cde' => 'CDE'
  }

Here is the view:

<%= f.input :kind, :collection => ProductComponent::KINDS.sort.invert %>
@rreynier

This comment has been minimized.

Copy link

commented Nov 19, 2013

@mahemoff Actually, I just figured it out. I had to invert then sort. Doing sort first was causing the error.

So: <%= f.input :kind, :collection => ProductComponent::KINDS.invert.sort%>

@uriklar

This comment has been minimized.

Copy link

commented Dec 1, 2013

Perfect! exactly what i was looking for. Thanks!

@akkdio

This comment has been minimized.

Copy link

commented Feb 21, 2014

Appreciate the post. Saved me.

@bmcharek

This comment has been minimized.

Copy link

commented Jun 2, 2014

6 months later, still very useful post. Thanks viola and others :).

update for my particular case

Since I needed some translation, only half of my problem was solved. So I ran into this gem https://github.com/brainspec/enumerize and voila. I don't need a collection anymore for my simple_form.

@jonnyjava

This comment has been minimized.

Copy link

commented Jun 25, 2014

Inspired by this great gist I did the following

MY_LIST = ["Choose:", "On-Campus Recruiting - CSO", "CSO Staff Referral","Faculty Contact", "Career Day", "CSO Summer Job Listing", "Alumni Contact", "Personal Contact", "Other"]
HASH_NAME = Hash[MY_LIST.map.with_index{ |obj, i| [i, obj] }]

This let me manage the list separately and add elements if needed!

@seoyoochan

This comment has been minimized.

Copy link

commented Jul 5, 2014

I appreciate that!

@zx1986

This comment has been minimized.

Copy link

commented Dec 14, 2014

感謝你!thanks for the tip!

@sankalpsingha

This comment has been minimized.

Copy link

commented Jan 27, 2015

Thank you so much for this!! Saved me a ton of time! :)

@phantom42

This comment has been minimized.

Copy link

commented Feb 11, 2015

Exactly what I needed. Thanks!

@carlflor

This comment has been minimized.

Copy link

commented Sep 4, 2015

wonderful! thank you!

@robinjfisher

This comment has been minimized.

Copy link

commented Jan 14, 2016

Thanks for this. Very useful!

@abaldwin88

This comment has been minimized.

Copy link

commented Apr 13, 2016

sort method is no longer needed in ruby >1.9

Hashes enumerate their values in the order that the corresponding keys were inserted.
http://ruby-doc.org/core-1.9.3/Hash.html

@irenemoreno

This comment has been minimized.

Copy link

commented Jun 4, 2016

Five years later... it has been really helpful. Gracias!

@jonathanroehm

This comment has been minimized.

Copy link

commented Jun 17, 2016

Amazingly helpful. Saved a ton of time. Thank you!

@araslanov-e

This comment has been minimized.

Copy link

commented Sep 30, 2016

f.input :some_field, :collection => Model::HASH_NAME, value_method: :first, label_method: :last

@mark-craig

This comment has been minimized.

Copy link

commented Jan 6, 2017

6 years later! Thank you so much!

@evanlouden

This comment has been minimized.

Copy link

commented Sep 29, 2017

Again. Thanks! Exactly what I needed today.

@shamca65

This comment has been minimized.

Copy link

commented Dec 26, 2018

Thanks! - Helped me troubleshoot getting my MDB select working properly:
"


<%= options_for_select(Customer::PROVINCES.map {|k,v| [v,k]}) %>

"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.