【问题标题】:Rails Carmen get Country name and State name from codeRails Carmen 从代码中获取国家名称和州名称
【发布时间】:2012-07-27 15:01:23
【问题描述】:

我正在使用 Carmen gem 在 Rails 中呈现国家/地区选择。国家在数据库中存储为代码。如何从此代码中取回“全名”?

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

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

我尝试了几个选项,到目前为止都没有奏效!例如:

Carmen::country_name(@profile.country)

导致错误:

Carmen:Module 的未定义方法 `country_name'

第二次尝试:

在profiles_controller中:

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

在配置文件索引视图中:

<%= @country.official_name %>

这会导致错误:

nil:NilClass 的未定义方法 `official_name'

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

【问题讨论】:

    标签: ruby-on-rails carmen


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这是国家名称

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

      对于州名

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

      希望这个答案是完整的。

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多