问题
I am trying to use the Countries Gem, but had some basic questions on how to incorporate this gem after I've bundle-installed it.
- Do I need to create a new controller/model to access the countries?
- How do I create a simple select drop-down to show a list of countries for a user to select?
- Where are all the countries stored? (I saw the data file in the Gem, but need some clarity how to bring that into my own app)
回答1:
1) You don't need a new controller/model to access the countries
2) There is an example app on the README page which shows you how to use forms and dropdowns.
3) The countries are stores within the app. I believe country_select includes the ISO 3166 gem to get the list of countries. You can view the data in the countries.yaml file
If want to know anything else, I recommend looking at the example app. It provides a good example of how to use the gem.
回答2:
You do not need to create a new controller/model to work with the gem.
In order for you to create dropdown, just install the country_select
gem (as stated in the doc)
Then to use it, just do that in your views:
country_select(:your_model_name, :your_attribute_name)
To integrate it in a form_for
with some extra params like Bootstrap classes or default country selected:
<%= form_for @message, url: contact_path, html: { :class => "form-horizontal " } do |f| %>
<div class="form-group">
<%= f.label :country, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.country_select :country, ["United States"], {}, { :class => "form-control" } %>
</div>
</div>
<%= f.submit "Submit", :class => "btn btn-default" %>
<% end %>
For the exact options you have w/ this method, see here:
country_select(method, priority_or_options = {}, options = {}, html_options = {})
Hope it helps !
来源:https://stackoverflow.com/questions/26007902/how-to-use-the-countries-gem