2015-10-17 145 views
0

我正在使用國家寶石和國家選擇的寶石。導軌:電話號碼 - 國際前綴

我在我的手機表中有一個名爲country_code的屬性。我要求用戶在新的手機窗體中選擇他們的國家:

<%= f.input :country_code, label: false, wrapper: :vertical_input_group do %> 
        <span class="input-group-addon">Country</span> 
       <%= country_select("phone", "country_code", { priority_countries: ["AU", "GB", "NZ"], selected: "AU" }, class: "form-control") %> 
       <% end %> 

我想拍攝該國家,並獲取國際前綴。

我有一個手機模型,其中我有:

def landline_number 
     [international_code, area_code, phone_number].join(' ') 
    end 

    def international_code 
     self.country_code = ISO3166::Country[country_code] 
     country_code.translations[I18n.locale.to_s] || country_code.international_prefix 
    end 

當我嘗試:

<%= @phone.landline_number %> 

它打印的國名,而不是國際前綴。

如何讓它打印一個數字而不是國家名稱?

回答

1

在我看來,你的問題是這一行:

country_code.translations[I18n.locale.to_s] || country_code.international_prefix 

根據文檔的例子

# Get a specific translation 
c.translation('de') #=> 'Vereinigte Staaten von Amerika' 
c.translations['fr'] #=> "États-Unis" 

ISO3166::Country.translations    # {"DE"=>"Germany",...} 
ISO3166::Country.translations('DE')  # {"DE"=>"Deutschland",...} 

你實際上是在提取該國的名頭,並因爲它的發現代碼

country_code.international_prefix 

未執行,因爲||。你有條件。如果您將刪除||的左側的代碼它應該工作。換句話說,試着離開你的方法是這樣的:

def international_code 
    self.country_code = ISO3166::Country[country_code] 
    country_code.international_prefix 
end