Rails Carmen从代码中获取国家名称和州名称

Mai*_*itz 3 ruby-on-rails carmen

我正在使用Carmen gem在Rails中渲染一个国家/地区.该国家/地区作为代码存储在数据库中.如何从此代码中获取"全名"?

宝石'carmen-rails','〜> 1.0.0.beta3'

<%= f.country_select :country, prompt: 'Please select a country' %>
Run Code Online (Sandbox Code Playgroud)

我尝试了几种选择,到目前为止还没有一种方法可行!例如:

Carmen::country_name(@profile.country)
Run Code Online (Sandbox Code Playgroud)

导致错误:

卡门的未定义方法`country_name':模块

第二次尝试:

在profiles_controller中:

@profile = Profile.find(params[:id])
@country = Carmen::Country.named( @profile.country )  
Run Code Online (Sandbox Code Playgroud)

在配置文件索引视图中

<%= @country.official_name %>
Run Code Online (Sandbox Code Playgroud)

这会导致错误:

未命名的方法`official_name'代表nil:NilClass

为什么我的应用程序失败了?

kev*_*itt 9

在github上查看carmen的测试规范:

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

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

<%= @country.name %>
Run Code Online (Sandbox Code Playgroud)