I am trying to make an app in Rails 4. I use simple form for forms and country_select gem for country lists.
I have an address model, which includes this method:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
When I try to save a new address, I get this error:
undefined method `translations' for "Australia":String
Can anyone see what's wrong with this method definition?
If I change my view to:
<% if @profile.addresses.any? %>
<%= @profile.addresses.first.country.titlecase %>
<% else %>
<span class="profileeditlink">
<%= link_to "Add your location", new_address_path %>
</span>
<% end %>
Then the record displays - but as AU instead of Australia (which is what the method in the address model provides).
Address table:
create_table "addresses", force: :cascade do |t|
t.string "unit"
t.string "building"
t.string "street_number"
t.string "street"
t.string "city"
t.string "region"
t.string "zip"
t.string "country"
t.boolean "main_address"
t.boolean "project_offsite"
t.string "time_zone"
t.float "latitude"
t.float "longitude"
t.integer "addressable_id"
t.integer "addressable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "addresses", ["addressable_type", "addressable_id"], name: "index_addresses_on_addressable_type_and_addressable_id", unique: true, using: :btree
TAKING JAEHYEN'S SUGGESTION,
I changed my country name method in the address model to:
def country_name
# self.country = ISO3166::Country(country)
# country.translations[I18n.locale.to_s] || country.name
iso_country = ISO3166::Country.find_by_name[country] # `country` should be name like 'Australia'
iso_country.translations[I18n.locale.to_s] || iso_country.name
end
I get this error:
undefined method `translations' for nil:NilClass
ANOTHER ATTEMPT:
I found this resource: http://www.scriptscoop.net/t/4ee6d5ef4577/displaying-countries-using-country-select-gem-in-rails-4.html
I tried changing my form input to:
<%= f.country_select :country, priority: [ "Australia", "New Zealand", "United Kingdom" ] %>
It still just displays the country code instead of the country name. I'm stuck.
ANOTHER ATTEMPT
I found this post: Rails Simple_Form: How to show long name of the country
The answer in this post suggests defining country_name as:
def country_name
country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
This is slightly different to my previous attempts, however, when I try this, I get this error:
undefined local variable or method `country_code' for #<Address:0x007fbae5bfb290>
I tried changing the method to:
def country_name
self.country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
This gives the same error as the formulation that does not use 'self'. I think these attempts don't work because the attribute in my address table is called 'country'.
When i change the method to:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
I get the error with the word 'translations'. When I delete '.translations' from the method, I get an error with the word 'name'.
I'm losing my marbles trying to figure this out.
ANOTHER ATTEMPT
I tried adding the countries gem to my gem file (above country_select).
Nothing changes when I bundle this gem. Same problem.
ANOTHER ATTEMPT
Trying again (as I originally had the method defined), but with countries gem installed (above country_select gem):
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
I get this error: undefined method `translations' for "Cayman Islands":String
This is the same problem that I originally started with, so I don't think that adding the countries gem has helped advance toward a solution.