My setup: Rails 3.0.9, Ruby 1.9.2
I have a global constant defined
TYPES = { "visa" => "Visa", "master" => "MasterCard" }
I wish to invert the values for the select method, I know it sounds silly but there is another part of my code that needs this functionality, so I’m trying to figure out if it is possible. Here’s what I have so far but didn’t work
<%= f.select :card_type, TYPES.each { |key, value| [value, key] } %>
Use the
invertmethod built-in toHash:So:
Notes:
:masterinstead of"master"TYPESwill be used as the keys for theinvert‘edHash, so ensure that your values are all unique.