Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Countries gem

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.

  1. Do I need to create a new controller/model to access the countries?
  2. How do I create a simple select drop-down to show a list of countries for a user to select?
  3. 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)
like image 221
gitastic Avatar asked Oct 20 '22 01:10

gitastic


2 Answers

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.

like image 80
Tom Kadwill Avatar answered Oct 23 '22 00:10

Tom Kadwill


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 !

like image 40
rorofromfrance Avatar answered Oct 22 '22 23:10

rorofromfrance