Question

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?

Était-ce utile?

La 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 %>

Autres conseils

This is for country name

@country = Carmen::Country.coded(@profile.country)

<%= @country.name %>

And for State Name

@subregions = @country.subregions
@state = @subregions.coded(@profile.state_code)
<%= @state.name %>

Hope this answer is use full.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top