Rails Select Drop Down for State?

I was wondering, maybe there is some kind of already built-in function for rails, so that it will create a drop-down list with all the states of the US state, so I won’t have to enter it manually. I searched on the Internet, but I could not find. Any suggestions on what to do, I don’t need to manually enter all the states?

+64
ruby ruby-on-rails drop-down-menu
Jun 19 2018-11-11T00:
source share
11 answers

some auxiliary file

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 

in some form

 <%= select_tag :state, options_for_select(us_states) %> 
+134
Jun 19 2018-11-11T00:
source share

Thanks Codeglot. In case someone wants to display an abbreviation from a 2-letter state instead of a full name:

 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 
+30
Aug 14 '12 at 16:22
source share

For this, I usually use Carmen and Carmen-Rails gems.

https://github.com/jim/carmen

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

Since my projects are still on Ruby 1.8, I have to use the specific ruby-18 branch, so there is the following in my Gemfile:

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

Then, to create a select tag for all US states in the form where you edit the field: state_code of the object: address model ...

 subregion_select(:address, :state_code, Carmen::Country.coded('US')) 
+17
Dec 19 '12 at 19:04
source share

This is a more detailed walkthrough. I am using Rails 4:

In the helpers folder, I created state_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 

In config → configurations, I add the following internal development.rb and production.rb

 config.action_controller.include_all_helpers = true 

Finally, inside my view I put (this is output in 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")) 

"CA" pre-selects California from the drop-down menu at boot.

NOTE. I have NOT used select_tag . Using this, I gave an undefined method error for select_tag (is select_tag in Ruby guides, how can it be undefined?) Using only select , it launched it.

+17
Mar 25 '14 at 16:20
source share

If this file does not work:

 <%= select_tag :state, us_states%> 

Try the following:

  <%=select_tag 'State', options_for_select(us_states),:name=>"state",:id=>"state"%> 
+3
Mar 28 '13 at 10:42 on
source share

I found a problem using the helper to contain states. It works great when creating a new record, but if I want to edit an existing record, I want the state in the database to be preselected from the drop-down list. I could not get this to work with the help of an assistant. But it works if you create a table of simple states. Here is what worked for me:

Create a state table for options of the selection window

Create a model file and state database file that has only columns for state_code and state_name (or whatever you name). rails g model State state_code:string:uniq state_name:string --no-timestamps --no-test-framework . This will create a migration file in the db / migrate folder. If you do not need an identifier column, you can edit it by inserting , id: false in the declaration of the create_table block.

 # 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 

And migrate the rake db:migrate database.

You can fill in the table using the semester file. Be sure to delete or comment out previously loaded data in the sample file so that you do not add duplicates.

 #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' } ]) 

Then run the rake command to sow db rake db:seed

In your form, you can add this as your selection box (I use state_code as the name of the field, but you can do it just a state or whatever):

 <%= 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'}) %> 

The format of the collection_select helper method in the Rails form block is f.collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) . If you want state_code to be both the text and the value of the drop-down list, change: state_name to: state_code in the first select argument and in text_method (note that the text and order of values ​​are canceled). In the parameters, I previously selected "CA", but I do this only for the new form, and not for editing (or it will override the value with CA every time). You can change this to an empty {include_blank: true} or add a prompt {prompt: 'Select State'} or just use the selected or first value with an empty hash {} by default. If you want to make the required field, you can add this to the html {class: 'form-control', required: true} parameters

Now in the form you can fill it out from the state table, and it will select the value when editing the record.

+3
Oct 27 '15 at 1:10
source share

To make this work with simple_form , I did it.

Added this to my user.rb model:

 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'] ] 

Made simple_form in my view as follows:

 <%= simple_form_for(@user) do |f| %> <%= f.input :state, as: :select, collection: User::STATES %> <%= f.button :submit %> <% end %> 
+3
Dec 06 '15 at 1:15
source share

You have a gem that can help you: a strange gem that integrates with country_select , so you have a complete solution for entering states.

+2
Aug 26 '13 at 15:57
source share

Check out https://rubygems.org/gems/country_state_select

Country State Select is a library that provides a simple API for creating Country, State / Province, and City drop-down lists for use in forms.

When implemented correctly, the State / Province drop-down menu is populated with the appropriate regions depending on the country that the user has selected.

For example, if the user selects “United States of America” for the Country drop-down list, the state drop-down menu is populated with 50 relevant states plus the District of Columbia, then the user can list the city according to the state’s choice, but currently cities are limited.

+1
Oct 30 '15 at 18:22
source share

I don't know if there is something built-in Rails to create an HTML select box populated with USA states

But here you have a screencast that explains this: http://railscasts.com/episodes/88-dynamic-select-menus

Hope this will be helpful.

0
Jun 19 2018-11-11T00:
source share

I created a sample project with detailed instructions on how to create drop-down lists in Rails 4.2.2 and Ruby 2.2.2 https://rubyplus.com/articles/2501

0
Jun 22 '15 at 8:55
source share



All Articles