2017-03-02 63 views
1

未定義的方法`地圖」我改變了格式爲datetime字段在Rails從到datetime和它引起的誤差,如下:扶手:引起缺失的I18n翻譯

undefined method `map' for "translation missing: zh-CN.date.order":String 

視圖代碼使上述錯誤是:

<%= f.input :start_time, :label => t("Start Time"), required: true, :as => :datetime, :ampm => true, :minute_step => 10, :start_year => Date.today.year - 1, :end_year => Date.today.year + 1, :format => 'YYYY/MM/DD/HH/MM', :use_month_numbers => true, :include_blank => true %> 

RAILS源代碼炸燬是在actionview/helpers/date_helper.rb

def translated_date_order 
    date_order = I18n.translate(:'date.order', :locale => @options[:locale], :default => []) 
    date_order = date_order.map { |element| element.to_sym } #<<<<<<===blows up 

    forbidden_elements = date_order - [:year, :month, :day] 
    if forbidden_elements.any? 
    raise StandardError, 
     "#{@options[:locale]}.date.order only accepts :year, :month and :day" 
    end 

    date_order 
end 

我確實有一個文件zh-CN.yml/config/locale/下,它爲其他人提供翻譯,除了這一個。的zh-CN.yml

UPDATE部分:

zh-CN: 

    #maint_recordx 
    Mfg Batches : '訂單批次一覽' 
    New Batch : '新批次' 
    Update Batch : '更新批次' 
    Edit Batch : '更新批次' 
........... 
+0

你可以顯示一個.yml結構嗎?可能是一個小的錯字 –

+0

你的助手方法有一個錯字:':'date.order''。刪除':',它應該只是一個字符串。如果解決了這個問題,我建議你刪除這個問題,因爲這裏不再適用。 – mmichael

+0

'mmichael',幫助器方法直接從/ actionview /下的Rails源代碼複製。還刪除它並沒有幫助。 – user938363

回答

1

通過此相同的錯誤被叮咬後,我發現,滑軌設置以下項:

:'date.order' 

爲值:

["year", "month", "day"] 

默認:en區域設置

您可以通過運行軌道控制檯下面的代碼段用於默認軌確認此安裝:

date_order = I18n.translate(:'date.order', :locale => :en, :default => []) 

通知我剛換@options[:locale]默認:en

軌道幫手您參考,希望的數組的date_order值,並且如果它沒有得到,它將炸燬。

在我的情況下,我錯誤地配置了I18n :: Backend :: ActiveRecord gem,因此它干擾了I18n返回的值。您可能有類似的問題,阻止返回:'date.order'密鑰的正確值。

編輯:

爲了解決這個問題,你應該只需要安裝寶石「rails-i18n'。它將處理返回支持的語言環境的正確日期格式。在我的情況下,我在我的es.yml語言環境文件中有一個自定義配置,它返回了不正確的日期格式。

+0

@opotions [:locale] ='zh-CN'導致錯誤。但如何解決它?我的配置如下:I18n.default_locale ='zh-CN'如果Rails.env.production? || Rails.env.development?在'/ initializers /'下的local.rb中 – user938363