How to use the Countries gem

后端 未结 2 766
抹茶落季
抹茶落季 2021-02-10 00:00

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 contro
相关标签:
2条回答
  • 2021-02-10 00:04

    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 !

    0 讨论(0)
  • 2021-02-10 00:22

    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.

    0 讨论(0)
提交回复
热议问题