4

In my Rails application, I installed the following gems

gem 'countries'
gem 'country_select'
gem 'simple_form'

When a user signs up, they select a country (ex. United Kingdom)

on the User's Show page `

<%= @user.country %>` => Displays GB

My question is, how do I display United Kingdom as full name?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mary Smith
  • 121
  • 2
  • 10

3 Answers3

5

From the countries gem's github page:

This gem automatically integrates with country_select. It will change its behavior to store the alpha2 country code instead of the country name.

Then, from country_select's github page

class User < ActiveRecord::Base
  # Assuming country_select is used with User attribute `country_code`
  # This will attempt to translate the country name and use the default
  # (usually English) name if no translation is available

  def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end
end
sebkkom
  • 1,426
  • 17
  • 31
  • Note that you have to say `<%= @user.country_**name** %>` now. – sebkkom Nov 11 '14 at 15:11
  • i got same error when i type <%= @user.country.name %>, error was undefined method `name' for "GB":String – Mary Smith Nov 11 '14 at 15:36
  • @MarySmith It has to be `country_name`, not `country.name`. – sebkkom Nov 11 '14 at 15:58
  • 1
    after i change country_name, another error i got undefined local variable or method `country_code' for # – Mary Smith Nov 11 '14 at 16:01
  • @MarySmith This looks like it's coming from somewhere else, are you saying `@user.country_code` anywhere in your code? Also, it's probably a good idea to provide some of your code in the original question. – sebkkom Nov 11 '14 at 16:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64724/discussion-between-mary-smith-and-sebkomianos). – Mary Smith Nov 11 '14 at 16:15
  • @MarySmith I am in the chat. – sebkkom Nov 11 '14 at 17:59
  • @MarySmith May i know how you managed to solve this problem. I realise that you might have come up with a solution on the chat – Benjamin Jan 13 '15 at 16:21
  • @Benjamin She never turned up. Are you having the same problem? – sebkkom Jan 14 '15 at 14:05
  • @sebkomianos We got it sorted. Thank you for your response. – Benjamin Jan 14 '15 at 17:52
  • The country_select documentation is awful. What is the answer to this question? I have the above method in my model and call it with "@widget.country_name" and get a "undefined local variable or method `country_code'" error. – MSC Feb 20 '15 at 11:38
  • @MSC If you are getting a `undefined local variable country_code` error, then you have `country_code` somewhere in your code (model or controller). Check it. – sebkkom Feb 26 '15 at 11:54
  • did you figure this out? I have the same problem. The only place where my code has the words 'country_code' is inside the country_name definition (as set out above) – Mel Jan 07 '16 at 21:37
  • @Mel do you have both countries and country_select gems in your gemfile? – Timmy Von Heiss Jul 14 '16 at 22:02
  • @TimmyVonHeiss - yes – Mel Jul 15 '16 at 01:44
  • @Mel here is the solution I used. I could not get this one on this page to work either. http://stackoverflow.com/questions/34560638/rails-4-country-select-simple-form/34915060#34915060 see the accepted answer – Timmy Von Heiss Jul 15 '16 at 01:52
0

@user.country will return a country object, ie an instance of class Country. When you put this inside the erb tag, it will have "to_s" called on it as rails tries its best to make a string out of an object. If the class defines a to_s method, then this is what is returned - i'm guessing it does and this method returns the country code.

You need to call the .name method on the country to get the name:

<%= @user.country.name %>
Max Williams
  • 32,435
  • 31
  • 130
  • 197
0

I believe the problem is to do with a missing gem here.

For this to work in your User model:

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

You need to have the "countries" gem installed as well as the country_select gem.