gem 'country_select', github: 'stefanpenner/country_select' is not working with rails 4

877 Views Asked by At

I am using gem 'country_select', github: 'stefanpenner/country_select' in my gem file and in my form i have defined it like this:

<%= form_for(@account_detail) do |f| %>

  <div class="field">
    <%= f.label :city %><br>
    <%= f.text_field :city %>
  </div>
  <div class="field">
    <%= f.label :zip %><br>
    <%= f.number_field :zip %>
  </div>
  <div class="field">
    <%= f.label :first_name %><br>
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br>
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :country %><br>
    <%= f.country_select("account_detail", "country") %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

on submit its giving error ActionView::Template::Error (wrong number of arguments (4 for 0)):

Which gem is best to show all countries?

3

There are 3 best solutions below

3
On BEST ANSWER

I would use:

<%= f.country_select :country %>

If you like to prioritize some countries in the select pass them in in an array:

 <%= f.country_select :country, {priority_countries: %w(<COUNTRY CODE i.e. US>), prompt: 'Select Country'} %>

You can add class: 'your-class' and id or whatever just as with any other field if you like. Hope it helps.

0
On

I have solved this issue by adding this method in my model:

def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end

and in view change given line :

<%= f.country_select("account_detail", "country") %>

to this:

<%= f.country_select :country, format: :with_alpha2 %>

Hope this will help to someone else who will face this problem.

1
On

This should do!

<%= f.country_select :country, { priority_countries: ["GB", "US"], selected: "GB" } %>