Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the list of countries into an array from country_select gem

I'm trying to extract a list of all country names and place them into an array from the country_select gem. It's very clear how to take advantage of this gem when it comes to adding a select field inside a form doing:

<%= form_for User.new, url: root_url do |f| %>
  <%= f.country_select :country_code %>
<% end %>

But what if I need to use that same country list outside of a form. In my case I'm using X-editable-rails gem for inline editing. My objective is to make inline edition available for a country attribute. To do that I'll have to specify the source of a select list as below:

<%= editable @waiter, :country, type: :select, title: 'Your country', source: ["Country_1", "Country_2", "Country_n"] %>

However, I can't come up with a way to extract that list and put it inside a variable.

I've tried these two suggested ways in this stack overflow answer:

@countries = ActionView::Helpers::FormOptionsHelper::COUNTRIES.zip(ActionView::Helpers::FormOptionsHelper::COUNTRIES)

and

<%= @countries = ActionView::Helpers::FormOptionsHelper::COUNTRIES %>

Could anyone help on how to achieve this?

like image 327
alopez02 Avatar asked Oct 24 '25 18:10

alopez02


1 Answers

country_select gem has countries gem as a dependency. The gem provides finders and other useful methods for working with countries.

ISO3166::Country.all_translated might be the method you're looking for.

like image 138
Igor Drozdov Avatar answered Oct 26 '25 09:10

Igor Drozdov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!