Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 955
Maier Moritz Avatar asked Dec 16 '22 21:12

Maier Moritz


1 Answers

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 %>
like image 138
kevinwmerritt Avatar answered Jan 14 '23 02:01

kevinwmerritt