导轨select下拉状态?

我想知道是否有一些已经build立在轨道的function,所以它会创build一个select下拉列表与所有美国国家,所以我不必手动input。 我在网上search,但我无法find任何。 任何build议如何做,所以我不必手动input所有的状态?

一些帮手文件

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 

以某种forms

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

感谢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 

为此我通常使用Carmen和Carmen-Railsgem。

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字段的窗体中为所有美国州创buildselect标记…

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

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

在助手文件夹下,我创build了states_helper.rb

在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 

在configuration – >环境下,我把以下内容在development.rb和production.rb

 config.action_controller.include_all_helpers = true 

最后,在我看来,我把(这是打字苗条的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")) 

加载时,“CA”在下拉菜单中预选加利福尼亚州。

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

如果这个不起作用:

 <%= select_tag :state, us_states%> 

尝试这个 :

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

我发现使用助手来包含状态的问题。 它创build一个新的logging完美的作品,但如果我想编辑一个现有的logging,我希望数据库中的状态预选在下拉框中。 我无法得到这个帮手的工作。 但是,如果你创build一个简单的状态表,它确实有效。 以下是对我有用的东西:

为select框选项创build一个状态表

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

 # 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

在你的表单中,你可以添加这个作为你的select框(我使用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(注意text和value的顺序相反)。 在我预选的选项“CA”的选项,但只做一个新的表单不编辑(或者它会重写每次CA值)。 您可以将其更改为空白{include_blank: true}或添加提示{prompt: 'Select State'}或者只是将其默认设置为具有空散列的选定值或第一个值。 {} 如果你想使这个领域需要你可以添加到HTML选项{class: 'form-control', required: true}

现在在表单中,您可以从状态表中填充它,并且在编辑logging时预先select该值。

为了使这个工作与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 %> 

你有一个gem,可以帮助你:与country_select集成的国家gem ,所以你有一个完整的解决scheme,为国家input。

检查这个https://rubygems.org/gems/country_state_select

国家select是一个图书馆,提供一个简单的API来生成国家,州/省和城市下拉式表单使用。

如果实施正确,则根据用户select的国家/地区填写州/省下拉菜单。

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

我不知道是否有内置的Rails使美国各州充满HTMLselect字段。

但是这里有一个解释这个的截屏: http : //railscasts.com/episodes/88-dynamic-select-menus

我希望这会有用。

我已经创build了一个示例项目,详细介绍了如何在Rails 4.2.2和Ruby 2.2.2中创build下拉菜单https://rubyplus.com/articles/2501