I was wondering if maybe there was some already built in function for rails so that it would create a select drop down list with all the U.S. states so I wouldn't have to enter it manually. I searched online but I was unable to find any. Any suggestions on what to do so I don't have to manually enter all the states?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
I don't know if there is something built-in Rails to make a HTML select field filled with U.S.A. states.
But here you have a screencast which explains this: http://railscasts.com/episodes/88-dynamic-select-menus
I hope it will be useful.
Thanks Codeglot. In case anyone is wanting to display the 2-letter state abbreviation instead of the full name:
This is a more detailed walkthrough. I'm using Rails 4:
Under the helpers folder I created states_helper.rb
Inside states_helper.rb:
Under config -> environments I put the following inside development.rb and production.rb
Finally, inside my view I put (this is typed out in Slim HTML)
The "CA" pre-selects California in the dropdown menu on load.
NOTE: I did NOT use
select_tag
. Using it gave me an undefined method error forselect_tag
(select_tag is in the Ruby guides, how can it be undefined?) Using justselect
made it work.I found a problem with using a helper to contain the states. It works perfectly when creating a new record but if I want to edit an existing record I want the state in the database to be preselected in the dropdown box. I couldn't get that to work using the helper. But it does work if you create a simple states table. Here's what worked for me:
Create a states table for the select box options
Generate a State model file and database table that only has columns for state_code and state_name (or whatever you want to call them).
rails g model State state_code:string:uniq state_name:string --no-timestamps --no-test-framework
. This will generate a migration file in the db/migrate folder. If you don't want an id column you can edit it by inserting, id: false
into the create_table block declaration.And migrate the database
rake db:migrate
.You can populate the table using the seed file. Make sure to delete or comment out any previously loaded data in the seed file so you don't add duplicates.
Then run the rake task to seed the db
rake db:seed
In your form you can add this as your select box (I'm using state_code as the field name but you can make it just state or whatever you want):
The collection_select helper method format in a Rails form block is
f.collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
. If you want state_code as both the text and value of the dropdown box then change the :state_name to :state_code in the first select argument and in the text_method (note the text and value orders are reversed). In the options I preselected 'CA', but only do that for a new form not edit (or it will override the value with CA each time). You can change that to a blank{include_blank: true}
or add a prompt{prompt: 'Select State'}
or just have it default to the selected or first value with an empty hash{}
. If you want to make the field required you can add that to the html options{class: 'form-control', required: true}
Now in your form you can populate it from the states table and it will preselect the value when editing a record.
For this I typically use the Carmen and Carmen-Rails gems.
https://github.com/jim/carmen
https://github.com/jim/carmen-rails
Since my projects are still all on Ruby 1.8, I have to use the specific ruby-18 branch, so I have the following in my Gemfile:
Then, to create the select tag for all US states in a form where you're editing the :state_code field of an :address model object...
To get this to work with
simple_form
, I did this.Added this to my
user.rb
model:Made the simple_form in my view use that: