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.