How to use the Countries gem

后端 未结 2 765
抹茶落季
抹茶落季 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| %>
    
        
    <%= f.label :country, :class => "col-md-3 control-label" %>
    <%= f.country_select :country, ["United States"], {}, { :class => "form-control" } %>
    <%= 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 !

提交回复
热议问题