ruby-on-railscarmen

Rails Carmen get Country name and State name from code


I am using the Carmen gem to render a country select in Rails. The country is stored as Code in the database. How can I get the "Full name" back from this code?

gem 'carmen-rails', '~> 1.0.0.beta3'

<%= f.country_select :country, prompt: 'Please select a country' %>

I tried several options, none of them worked so far! For example:

Carmen::country_name(@profile.country)

Leading to error:

undefined method `country_name' for Carmen:Module

Second try:

in profiles_controller:

@profile = Profile.find(params[:id])
@country = Carmen::Country.named( @profile.country )  

In profiles index view:

<%= @country.official_name %>

This leads to error:

undefined method `official_name' for nil:NilClass

Why is my application failing?


Solution

  • Check out the test spec for carmen on github:

    https://github.com/jim/carmen/blob/master/spec/carmen/country_spec.rb

    @country = Carmen::Country.coded(@profile.country)
    
    <%= @country.name %>