开发者

Rails select helper method

My setup: Rails 3.0.9, Ruby 1.9.2

I have a global constant defined

TYPES = { "visa" => "Visa", "master" => "Ma开发者_开发百科sterCard" }

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 invert method built-in to Hash:

TYPES = { "visa" => "Visa", "master" => "MasterCard" }

TYPES.invert
# => {"Visa"=>"visa", "MasterCard"=>"master"}

So:

<%= f.select :card_type, TYPES.invert %>

Notes:

  1. Why not use symbols instead of strings for the key.. :master instead of "master"
  2. The values of original TYPES will be used as the keys for the invert'ed Hash, so ensure that your values are all unique.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜