开发者

Rails: populate a select list with US states and abbreviation, using Decoder::Countries[:US].states

Good evening all.

I'm attempting to dynamically populate a select_tag call in my rails view using Decoder::Countries to generate the list. My problem is that I can't get the options to come out in the format that I need.

The syntax:

Decoder::Countries[:US]

returns a hash of US states in the format of:

"AL" => "Alabama"

So in the view, doing this:

select_tag :tag_name, options_for_select(Decoder::Countries[:US].states.sort)

produces a select list that looks like this:

<select name="tag_name" id="tag_name">
  <option value="Alaska">AK</option>
  <option value="Alabama">AL</option>
  etc...

What I need is output like this:

<se开发者_Python百科lect name="tag_name" id="tag_name">
  <option value="al">Alaska</option>

Now, I know that appending .sort turns the hash into an array. How can I get it to display the output I need, or do I need to back this up into the controller some how?

Thanks.


Does inverting the hash work?

select_tag :tag_name, options_for_select(Decoder::Countries[:US].states.invert)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜