2013-11-26 149 views
1

我在country_select中使用了一個名爲simple_form的Ruby on Rails gem,我試圖讓下拉菜單顯示長的國家名稱,但將值設置爲短的iso代碼。simpleform and country_select with iso_codes

,如果我有所有我的國家重點國家繼

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

然後是正確的(顯示全名,而使用該值的iso_code)。優先國家雖然使用該名稱作爲標籤&值。有沒有辦法在優先國家設置ISO代碼?

回答

0

調查simple_form源代碼是一個真正的PITA,因爲它是如此令人難以置信的抽象。但從我所看到的情況來看,這似乎是使用CountrySelect 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 
+1

不得不升級我的寶石經測試,使用小寫僅在我的選擇行上包含iso_codes:true。通過simple_form工作。 – map7

1

只是通過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