Rails为国家选择下拉?

时间:2011-06-19 01:31:36

标签: ruby-on-rails ruby drop-down-menu

我想知道是否有一些已经内置的功能用于rails,因此它会创建一个包含所有美国州的选择下拉列表,所以我不必手动输入它。我在网上搜索但我找不到任何东西。有关如何做的任何建议我不必手动输入所有状态?

13 个答案:

答案 0 :(得分:140)

一些帮助文件

def us_states
    [
      ['Alabama', 'AL'],
      ['Alaska', 'AK'],
      ['Arizona', 'AZ'],
      ['Arkansas', 'AR'],
      ['California', 'CA'],
      ['Colorado', 'CO'],
      ['Connecticut', 'CT'],
      ['Delaware', 'DE'],
      ['District of Columbia', 'DC'],
      ['Florida', 'FL'],
      ['Georgia', 'GA'],
      ['Hawaii', 'HI'],
      ['Idaho', 'ID'],
      ['Illinois', 'IL'],
      ['Indiana', 'IN'],
      ['Iowa', 'IA'],
      ['Kansas', 'KS'],
      ['Kentucky', 'KY'],
      ['Louisiana', 'LA'],
      ['Maine', 'ME'],
      ['Maryland', 'MD'],
      ['Massachusetts', 'MA'],
      ['Michigan', 'MI'],
      ['Minnesota', 'MN'],
      ['Mississippi', 'MS'],
      ['Missouri', 'MO'],
      ['Montana', 'MT'],
      ['Nebraska', 'NE'],
      ['Nevada', 'NV'],
      ['New Hampshire', 'NH'],
      ['New Jersey', 'NJ'],
      ['New Mexico', 'NM'],
      ['New York', 'NY'],
      ['North Carolina', 'NC'],
      ['North Dakota', 'ND'],
      ['Ohio', 'OH'],
      ['Oklahoma', 'OK'],
      ['Oregon', 'OR'],
      ['Pennsylvania', 'PA'],
      ['Puerto Rico', 'PR'],
      ['Rhode Island', 'RI'],
      ['South Carolina', 'SC'],
      ['South Dakota', 'SD'],
      ['Tennessee', 'TN'],
      ['Texas', 'TX'],
      ['Utah', 'UT'],
      ['Vermont', 'VT'],
      ['Virginia', 'VA'],
      ['Washington', 'WA'],
      ['West Virginia', 'WV'],
      ['Wisconsin', 'WI'],
      ['Wyoming', 'WY']
    ]
end

以某种形式

<%= select_tag :state, options_for_select(us_states) %>

答案 1 :(得分:30)

谢谢Codeglot。如果有人想要显示2个字母的州名缩写而不是全名:

def us_states
  [
    ['AK', 'AK'],
    ['AL', 'AL'],
    ['AR', 'AR'],
    ['AZ', 'AZ'],
    ['CA', 'CA'],
    ['CO', 'CO'],
    ['CT', 'CT'],
    ['DC', 'DC'],
    ['DE', 'DE'],
    ['FL', 'FL'],
    ['GA', 'GA'],
    ['HI', 'HI'],
    ['IA', 'IA'],
    ['ID', 'ID'],
    ['IL', 'IL'],
    ['IN', 'IN'],
    ['KS', 'KS'],
    ['KY', 'KY'],
    ['LA', 'LA'],
    ['MA', 'MA'],
    ['MD', 'MD'],
    ['ME', 'ME'],
    ['MI', 'MI'],
    ['MN', 'MN'],
    ['MO', 'MO'],
    ['MS', 'MS'],
    ['MT', 'MT'],
    ['NC', 'NC'],
    ['ND', 'ND'],
    ['NE', 'NE'],
    ['NH', 'NH'],
    ['NJ', 'NJ'],
    ['NM', 'NM'],
    ['NV', 'NV'],
    ['NY', 'NY'],
    ['OH', 'OH'],
    ['OK', 'OK'],
    ['OR', 'OR'],
    ['PA', 'PA'],
    ['RI', 'RI'],
    ['SC', 'SC'],
    ['SD', 'SD'],
    ['TN', 'TN'],
    ['TX', 'TX'],
    ['UT', 'UT'],
    ['VA', 'VA'],
    ['VT', 'VT'],
    ['WA', 'WA'],
    ['WI', 'WI'],
    ['WV', 'WV'],
    ['WY', 'WY']
  ]
end

答案 2 :(得分:18)

为此,我通常使用Carmen和Carmen-Rails宝石。

https://github.com/jim/carmen

https://github.com/jim/carmen-rails

由于我的项目仍然在Ruby 1.8上,我必须使用特定的ruby-18分支,所以我的Gemfile中有以下内容:

gem 'carmen', :git => 'git://github.com/jim/carmen.git', :tag => 'ruby-18'
gem 'carmen-rails', :git => 'git://github.com/jim/carmen-rails.git'

然后,在您正在编辑地址模型对象的:state_code字段的表单中为所有美国州创建选择标记...

subregion_select(:address, :state_code, Carmen::Country.coded('US'))

答案 3 :(得分:18)

这是一个更详细的演练。我使用的是Rails 4:

在helpers文件夹下,我创建了states_helper.rb

inside states_helper.rb:

module StatesHelper

def us_states
  [
    ['Alabama', 'AL'],
    ['Alaska', 'AK'],
    ['Arizona', 'AZ'],
    ['Arkansas', 'AR'],
    ['California', 'CA'],
    ['Colorado', 'CO'],
    ['Connecticut', 'CT'],
    ['Delaware', 'DE'],
    ['District of Columbia', 'DC'],
    ['Florida', 'FL'],
    ['Georgia', 'GA'],
    ['Hawaii', 'HI'],
    ['Idaho', 'ID'],
    ['Illinois', 'IL'],
    ['Indiana', 'IN'],
    ['Iowa', 'IA'],
    ['Kansas', 'KS'],
    ['Kentucky', 'KY'],
    ['Louisiana', 'LA'],
    ['Maine', 'ME'],
    ['Maryland', 'MD'],
    ['Massachusetts', 'MA'],
    ['Michigan', 'MI'],
    ['Minnesota', 'MN'],
    ['Mississippi', 'MS'],
    ['Missouri', 'MO'],
    ['Montana', 'MT'],
    ['Nebraska', 'NE'],
    ['Nevada', 'NV'],
    ['New Hampshire', 'NH'],
    ['New Jersey', 'NJ'],
    ['New Mexico', 'NM'],
    ['New York', 'NY'],
    ['North Carolina', 'NC'],
    ['North Dakota', 'ND'],
    ['Ohio', 'OH'],
    ['Oklahoma', 'OK'],
    ['Oregon', 'OR'],
    ['Pennsylvania', 'PA'],
    ['Puerto Rico', 'PR'],
    ['Rhode Island', 'RI'],
    ['South Carolina', 'SC'],
    ['South Dakota', 'SD'],
    ['Tennessee', 'TN'],
    ['Texas', 'TX'],
    ['Utah', 'UT'],
    ['Vermont', 'VT'],
    ['Virginia', 'VA'],
    ['Washington', 'WA'],
    ['West Virginia', 'WV'],
    ['Wisconsin', 'WI'],
    ['Wyoming', 'WY']
  ]
end
end

在配置下 - &gt;环境我把以下内容放在development.rb和production.rb

config.action_controller.include_all_helpers = true

最后,在我的视图中,我把(这是在Slim HTML中输入的)

= form_for :order_submissions, url: order_url, html: { id: "order_form"} do |f|
fieldset
.form-group
  = f.select(:state, options_for_select(us_states, "CA"))

&#34; CA&#34;在加载时在下拉菜单中预先选择加利福尼亚。

注意:我没有使用select_tag。使用它给了select_tag一个未定义的方法错误(select_tag在Ruby指南中,怎么可以不定义?)只使用select使它工作。

答案 4 :(得分:6)

为了让这与simple_form一起使用,我做到了。

将此添加到我的user.rb模型中:

STATES = 
  [
    ['Alabama', 'AL'],
    ['Alaska', 'AK'],
    ['Arizona', 'AZ'],
    ['Arkansas', 'AR'],
    ['California', 'CA'],
    ['Colorado', 'CO'],
    ['Connecticut', 'CT'],
    ['Delaware', 'DE'],
    ['District of Columbia', 'DC'],
    ['Florida', 'FL'],
    ['Georgia', 'GA'],
    ['Hawaii', 'HI'],
    ['Idaho', 'ID'],
    ['Illinois', 'IL'],
    ['Indiana', 'IN'],
    ['Iowa', 'IA'],
    ['Kansas', 'KS'],
    ['Kentucky', 'KY'],
    ['Louisiana', 'LA'],
    ['Maine', 'ME'],
    ['Maryland', 'MD'],
    ['Massachusetts', 'MA'],
    ['Michigan', 'MI'],
    ['Minnesota', 'MN'],
    ['Mississippi', 'MS'],
    ['Missouri', 'MO'],
    ['Montana', 'MT'],
    ['Nebraska', 'NE'],
    ['Nevada', 'NV'],
    ['New Hampshire', 'NH'],
    ['New Jersey', 'NJ'],
    ['New Mexico', 'NM'],
    ['New York', 'NY'],
    ['North Carolina', 'NC'],
    ['North Dakota', 'ND'],
    ['Ohio', 'OH'],
    ['Oklahoma', 'OK'],
    ['Oregon', 'OR'],
    ['Pennsylvania', 'PA'],
    ['Puerto Rico', 'PR'],
    ['Rhode Island', 'RI'],
    ['South Carolina', 'SC'],
    ['South Dakota', 'SD'],
    ['Tennessee', 'TN'],
    ['Texas', 'TX'],
    ['Utah', 'UT'],
    ['Vermont', 'VT'],
    ['Virginia', 'VA'],
    ['Washington', 'WA'],
    ['West Virginia', 'WV'],
    ['Wisconsin', 'WI'],
    ['Wyoming', 'WY']
  ]

在我的视图中使用simple_form使用:

<%= simple_form_for(@user) do |f| %>    
    <%= f.input :state, as: :select, collection: User::STATES %>
    <%= f.button :submit %>
<% end %>

答案 5 :(得分:5)

我发现使用帮助程序来包含状态时出现问题。它在创建新记录时非常有效,但如果我想编辑现有记录,我希望在下拉框中预选数据库中的状态。我无法使用助手来工作。但是如果你创建一个简单的状态表,它确实有效。这对我有用:

为选择框选项

创建状态表

生成状态模型文件和数据库表,该表只包含state_code和state_name的列(或任何您想要调用它们的列)。 rails g model State state_code:string:uniq state_name:string --no-timestamps --no-test-framework。这将在db / migrate文件夹中生成迁移文件。如果您不想要id列,可以通过将, id: false插入create_table块声明来编辑它。

# db/migrate/timestamp_create_states.rb
class CreateStates < ActiveRecord::Migration
  def change
    create_table :states, id: false do |t|
      t.string :state_code, null: false
      t.string :state_name
    end
    add_index :states, :state_code, unique: true
  end
end

迁移数据库rake db:migrate

您可以使用种子文件填充表格。确保删除或注释掉种子文件中以前加载的任何数据,这样就不会添加重复数据。

#db/seeds.rb
states = State.create!([
  { state_name: 'Alaska', state_code: 'AK' },
  { state_name: 'Alabama', state_code: 'AL' },
  { state_name: 'Arkansas', state_code: 'AR' },
  { state_name: 'Arizona', state_code: 'AZ' },
  { state_name: 'California', state_code: 'CA' },
  { state_name: 'Colorado', state_code: 'CO' },
  { state_name: 'Connecticut', state_code: 'CT' },
  { state_name: 'District of Columbia', state_code: 'DC' },
  { state_name: 'Delaware', state_code: 'DE' },
  { state_name: 'Florida', state_code: 'FL' },
  { state_name: 'Georgia', state_code: 'GA' },
  { state_name: 'Hawaii', state_code: 'HI' },
  { state_name: 'Iowa', state_code: 'IA' },
  { state_name: 'Idaho', state_code: 'ID' },
  { state_name: 'Illinois', state_code: 'IL' },
  { state_name: 'Indiana', state_code: 'IN' },
  { state_name: 'Kansas', state_code: 'KS' },
  { state_name: 'Kentucky', state_code: 'KY' },
  { state_name: 'Louisiana', state_code: 'LA' },
  { state_name: 'Massachusetts', state_code: 'MA' },
  { state_name: 'Maryland', state_code: 'MD' },
  { state_name: 'Maine', state_code: 'ME' },
  { state_name: 'Michigan', state_code: 'MI' },
  { state_name: 'Minnesota', state_code: 'MN' },
  { state_name: 'Missouri', state_code: 'MO' },
  { state_name: 'Mississippi', state_code: 'MS' },
  { state_name: 'Montana', state_code: 'MT' },
  { state_name: 'North Carolina', state_code: 'NC' },
  { state_name: 'North Dakota', state_code: 'ND' },
  { state_name: 'Nebraska', state_code: 'NE' },
  { state_name: 'New Hampshire', state_code: 'NH' },
  { state_name: 'New Jersey', state_code: 'NJ' },
  { state_name: 'New Mexico', state_code: 'NM' },
  { state_name: 'Nevada', state_code: 'NV' },
  { state_name: 'New York', state_code: 'NY' },
  { state_name: 'Ohio', state_code: 'OH' },
  { state_name: 'Oklahoma', state_code: 'OK' },
  { state_name: 'Oregon', state_code: 'OR' },
  { state_name: 'Pennsylvania', state_code: 'PA' },
  { state_name: 'Puerto Rico', state_code: 'PR' },
  { state_name: 'Rhode Island', state_code: 'RI' },
  { state_name: 'South Carolina', state_code: 'SC' },
  { state_name: 'South Dakota', state_code: 'SD' },
  { state_name: 'Tennessee', state_code: 'TN' },
  { state_name: 'Texas', state_code: 'TX' },
  { state_name: 'Utah', state_code: 'UT' },
  { state_name: 'Virginia', state_code: 'VA' },
  { state_name: 'Vermont', state_code: 'VT' },
  { state_name: 'Washington', state_code: 'WA' },
  { state_name: 'Wisconsin', state_code: 'WI' },
  { state_name: 'West Virginia', state_code: 'WV' },
  { state_name: 'Wyoming', state_code: 'WY' }
])

然后运行rake任务以播种db rake db:seed

在您的表单中,您可以将其添加为您的选择框(我使用state_code作为字段名称,但您可以将其设置为州或任何您想要的内容):

<%= f.label :state_code, 'State', class: 'control-label' %>
<%= f.collection_select(:state_code, State.select(:state_name, :state_code),
   :state_code, :state_name, {selected: 'CA'}, {class: 'form-control'}) %>

Rails表单块中的collection_select辅助方法格式为f.collection_select(method, collection, value_method, text_method, options = {}, html_options = {})。如果您希望state_code同时作为下拉框的文本和值,则在第一个select参数和text_method中将:state_name更改为:state_code(请注意文本和值顺序相反)。在我预先选择的选项&#39; CA&#39;中,但仅对新表单不进行编辑(或者每次都会覆盖CA的值)。您可以将其更改为空白{include_blank: true}或添加提示{prompt: 'Select State'},或者将其默认为选定或第一个值为空哈希{}的值。如果您想要填写所需的字段,可以将其添加到html选项{class: 'form-control', required: true}

现在,您可以在表单中填写状态表,并在编辑记录时预先选择该值。

答案 6 :(得分:3)

如果这个不起作用:

<%= select_tag :state, us_states%>

试试这个:

 <%=select_tag 'State', options_for_select(us_states),:name=>"state",:id=>"state"%>

答案 7 :(得分:2)

您有一个可以帮助您的宝石:the countries gemcountry_select集成,因此您可以获得状态输入的完整解决方案。

此外,如果您想减少gem依赖关系列表,您可以这样做:

 <%= f.select :country_code, ::ISO3166::Country.all_names_with_codes,{ include_blank: true } %>

答案 8 :(得分:1)

选中此https://rubygems.org/gems/country_state_select

Country State Select是一个库,它提供了一个简单的API来生成国家,州/省和城市下拉列表,以便在表单中使用。

如果正确实施,州/省下拉列表将根据用户选择的国家/地区填充适当的区域。

例如,如果用户选择“美国”作为国家/地区下拉列表,则州下拉列表将填充50个适当的州以及哥伦比亚特区,然后用户可以根据州选择列出城市,但目前城市是有限的。

答案 9 :(得分:0)

我不知道是否有内置的Rails来创建一个填充了美国州的HTML选择字段。

但是在这里你有一个截屏视频解释了这个: http://railscasts.com/episodes/88-dynamic-select-menus

我希望它会有用。

答案 10 :(得分:0)

我创建了一个示例项目,其中包含有关如何在Rails 4.2.2和Ruby 2.2.2中创建下拉列表的详细说明https://rubyplus.com/articles/2501

答案 11 :(得分:0)

如果要保存全名状态

# frozen_string_literal: true

module StateHelper

  def us_states
    [
      ['Alabama'],
      ['Alaska'],
      ['Arizona'],
      ['Arkansas'],
      ['California'],
      ['Colorado'],
      ['Connecticut'],
      ['Delaware'],
      ['District of Columbia'],
      ['Florida'],
      ['Georgia'],
      ['Hawaii'],
      ['Idaho'],
      ['Illinois'],
      ['Indiana'],
      ['Iowa'],
      ['Kansas'],
      ['Kentucky'],
      ['Louisiana'],
      ['Maine'],
      ['Maryland'],
      ['Massachusetts'],
      ['Michigan'],
      ['Minnesota'],
      ['Mississippi'],
      ['Missouri'],
      ['Montana'],
      ['Nebraska'],
      ['Nevada'],
      ['New Hampshire'],
      ['New Jersey'],
      ['New Mexico'],
      ['New York'],
      ['North Carolina'],
      ['North Dakota'],
      ['Ohio'],
      ['Oklahoma'],
      ['Oregon'],
      ['Pennsylvania'],
      ['Puerto Rico'],
      ['Rhode Island'],
      ['South Carolina'],
      ['South Dakota'],
      ['Tennessee'],
      ['Texas'],
      ['Utah'],
      ['Vermont'],
      ['Virginia'],
      ['Washington'],
      ['West Virginia'],
      ['Wisconsin'],
      ['Wyoming']
    ]
  end
end

然后以表单

<%= f.select :state, options_for_select(us_states), class:"form-control", required: true %>

答案 12 :(得分:0)

使用哈希。我把我放在 config / initializers / us_states.rb 中,但是它可以在助手中或在您喜欢的其他任何地方工作:

US_STATES = {
  'AL': 'Alabama',
  'AK': 'Alaska',
  'AZ': 'Arizona',
  'AR': 'Arkansas',
  'CA': 'California',
  'CO': 'Colorado',
  'CT': 'Connecticut',
  'DE': 'Delaware',
  'DC': 'District of Columbia',
  'FL': 'Florida',
  'GA': 'Georgia',
  'HI': 'Hawaii',
  'ID': 'Idaho',
  'IL': 'Illinois',
  'IN': 'Indiana',
  'IA': 'Iowa',
  'KS': 'Kansas',
  'KY': 'Kentucky',
  'LA': 'Louisiana',
  'ME': 'Maine',
  'MD': 'Maryland',
  'MA': 'Massachusetts',
  'MI': 'Michigan',
  'MN': 'Minnesota',
  'MS': 'Mississippi',
  'MO': 'Missouri',
  'MT': 'Montana',
  'NE': 'Nebraska',
  'NV': 'Nevada',
  'NH': 'New Hampshire',
  'NJ': 'New Jersey',
  'NM': 'New Mexico',
  'NY': 'New York',
  'NC': 'North Carolina',
  'ND': 'North Dakota',
  'OH': 'Ohio',
  'OK': 'Oklahoma',
  'OR': 'Oregon',
  'PA': 'Pennsylvania',
  'PR': 'Puerto Rico',
  'RI': 'Rhode Island',
  'SC': 'South Carolina',
  'SD': 'South Dakota',
  'TN': 'Tennessee',
  'TX': 'Texas',
  'UT': 'Utah',
  'VT': 'Vermont',
  'VA': 'Virginia',
  'WA': 'Washington',
  'WV': 'West Virginia',
  'WI': 'Wisconsin',
  'WY': 'Wyoming'
}

然后,以我的形式:

<%= form.select :state, US_STATES.invert.sort %>

由于存储的是两个字母的代码,因此,如果要显示状态名称,我只需在哈希中引用两个字母的键即可:

<%= US_STATES[state] %>

如果您想变得更漂亮,请在语言环境文件中将其初始化(例如, config / locales / en.yml ):

---
en:
  us_states:
    AL: Alabama
    AK: Alaska
    AZ: Arizona
    # etc.

并通过以下方式在您的视图中访问它们:

<%= t "us_states.#{state}" %>

ℹ️当您在应用中支持多种语言时,这实际上与国家和语言代码一起使用效果更好。