【问题标题】:simpleform and country_select with iso_codes带有 iso_codes 的 simpleform 和 country_select
【发布时间】:2014-07-24 14:42:35
【问题描述】:

我正在使用一个名为 simple_form 和 country_select 的 Ruby on Rails gem,我试图让下拉菜单显示长国家名称,但将值设置为短 iso 代码。

如果我有以下情况

= f.input :country, priority: ["Australia", "United States", "New Zealand"]

然后我的优先国家之后的所有国家都是正确的(显示全名,同时使用 iso_code 作为值)。优先国家虽然使用名称作为标签和值。有没有办法在优先国家设置 ISO 代码?

【问题讨论】:

    标签: ruby-on-rails ruby simple-form


    【解决方案1】:

    你可以设置

    ::CountrySelect.use_iso_codes = true
    

    在初始化程序中全局使用 ISO 代码作为下拉值而不是国家/地区名称。

    http://rubydoc.info/gems/country_select/1.3.1/frames

    【讨论】:

      【解决方案2】:

      只需将iso_codes: true 传递给您的字段:

      = f.input :country, priority: ["Australia", "United States", "New Zealand"], iso_codes: true
      

      使用 simple_form 3.1.0.rc2 和 country_select 1.3.1 测试

      【讨论】:

        【解决方案3】:

        查看 simple_form 源代码是一个真正的 PITA,因为它是如此令人难以置信的抽象。但据我所知,这似乎是使用CountrySelect Gem 的问题。

        gem 说您应该使用iso_codes: true 来确保将代码用作密钥。我想您将不得不深入研究如何处理 priority 参数或尝试将此选项传递给 gem。

        以下是相关代码:

          def country_options_for_select(selected = nil, priority_countries = nil, use_iso_codes = false)
            country_options = "".html_safe
        
            if priority_countries
              priority_countries_options = if use_iso_codes || ::CountrySelect.use_iso_codes
                                             priority_countries.map do |code|
                                               [
                                                 ::CountrySelect::COUNTRIES[code],
                                                 code
                                               ]
                                             end
                                           else
                                             priority_countries
                                           end
        
              country_options += options_for_select(priority_countries_options, selected)
              country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n".html_safe
              #
              # prevents selected from being included
              # twice in the HTML which causes
              # some browsers to select the second
              # selected option (not priority)
              # which makes it harder to select an
              # alternative priority country
              #
              selected = nil if priority_countries.include?(selected)
            end
        
            values = if use_iso_codes || ::CountrySelect.use_iso_codes
                       ::CountrySelect::ISO_COUNTRIES_FOR_SELECT
                     else
                       ::CountrySelect::COUNTRIES_FOR_SELECT
                     end
        
            return country_options + options_for_select(values.sort, selected)
          end
        

        【讨论】:

        • 必须升级我的 gem,只使用小写的 alpha2 代码并在我的选择行中包含 iso_codes: true。通过 simple_form 工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多