I just want a simple country dropdown, prioritized for my purposes (ie to include the states within the country selected).
In fact, I only want 1 or 2 countries.
I am using Simple_Form and their documentation say that in order to get access to this:
f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]
I just need to include this in my Gemfile:
gem 'country_select'
But I am confused...I don't have to run a migration to store the country selection on my Post
model - which is the model I will be modifying my _form.html.erb
for?
When I simply do:
<%= f.input :country, as: :country, collection: [ "Australia", "Brazil", "New Zealand"] %>
I got a no country method on Post
error. I had to add this to my Post.rb
:
attr_accessor :country
Which if my memory serves me correctly, I should no longer have to do in Rails 4 because of Strong Parameters, right?
That works, but now the collection:
specification doesn't work. I still see a long list of countries.
Keep in mind I have not had to run any migrations to modify my Post.rb
model. I don't know what columns to add. Do I add say country, state, city
to my Post
model or do I create a new model called Country
?
Is all of this information about the countries (country code, cities, states, etc.) in the gem being loaded via a YAML file in the Gem?
I have no clue and the documentation is surprisingly sparse.
So my questions are simply these:
simple_form
collection to work. Post
record? Thanks.
The documentation for simple_form is for use with country_select 1.x. To use simple_form as documented, restrict your version of country_select in your gemfile:
gem 'country_select', '~> 1.0'
and then bundle update country_select
to downgrade.
If you want to use country_select 2.x instead, you'll be working with country codes, not country names. Your form helper will look a little different from the simple_form docs:
<%= f.input :country_code, as: :country, priority: ['BR'] %>
You do need to add a column to your Post model to store the country the user has selected in your database. The data type will be a string/varchar. You can generate the migration with something like:
rails generate migration AddCountryCodeToPost country_code:string
Not cities or states.
If you want users to supply states or cities, you'll either let them fill in text fields and store whatever they supply as strings (you'll add columns for these to your model as above), or build your own select/autocomplete system. There's a state_select gem, but you'll need to manually integrate it with country_select using javascript or a pipelined form. I'm not aware of any gems that provide data sources for city names by state/country, but there may very well be some out there.
For more on the differences between country_select 1.x and country_select 2.x, see the upgrade guide .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.