【问题标题】:Add a custom country field option to country_select ruby on rails gem向 country_select ruby​​ on rails gem 添加自定义国家/地区字段选项
【发布时间】:2015-10-12 15:25:08
【问题描述】:

我目前正在使用 country_select gem for ruby​​ on rails: https://github.com/stefanpenner/country_select

以下代码打印所有国家/地区的列表:

<%= f.country_select :CountryTo, html_options = {:class => "form-control"} %>

我想在选择下拉列表中添加一个自定义选项字段,其值为“Anywhere”。 有谁知道我怎么能做到这一点?我对 Ruby on rails 很陌生,所以如果有一个超级简单的修复方法,我深表歉意,谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 drop-down-menu


    【解决方案1】:

    根据tests of country_select,它支持select_taginclude_blank-option。所以你应该可以使用:

    <%= f.country_select(:country_to, include_blank: 'Anywhere') %>
    

    请考虑查看ruby style guide

    【讨论】:

      【解决方案2】:

      例如,只需添加这个初始化器:

      # config/initializers/00_add_countries.rb
      
      require 'countries'
      
      ISO3166::Data.register(
        alpha2: 'ww',
        name: 'World',
        translations: {
          'en' => 'World',
          'de' => 'Welt',
          'fr' => 'Monde'
        }
      )
      
      ISO3166::Country.new('ww').name == 'World'
      

      【讨论】:

        猜你喜欢
        • 2010-11-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 2015-04-09
        相关资源
        最近更新 更多