Search code examples
ruby-on-railsruby-on-rails-4simple-form

RAILS: undefined method `map' caused by missing I18n translation


I changed the format for a datetime field in a RAILS 4 new.html.erb from :string to datetime and it caused error as below:

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

The view code causing the error above is:

<%= 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 %>

The RAILS source code blows up is in 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

I do have a file zh-CN.yml under /config/locale/ and it is providing translations for others except this one.

UPDATE portion of zh-CN.yml:

zh-CN:

  #maint_recordx
  Mfg Batches  : '订单批次一览'
  New Batch  : '新批次'
  Update Batch : '更新批次'
  Edit Batch : '更新批次'
...........

Solution

  • After being bitten by this same error, I found that Rails sets the following key:

    :'date.order'
    

    to the value:

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

    for the default :en locale

    You can confirm this by running the following snippet in rails console for a default rails install:

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

    Notice I just switched @options[:locale] for the default :en value

    The rails helper you reference, expects an array for the date_order value, and will blow up if it doesn't get one.

    In my case, I improperly configured the I18n::Backend::ActiveRecord gem and therefore it interfered with the value being returned by I18n. You probably have a similar issue preventing the correct value for the :'date.order' key being returned.

    EDIT:

    In order to fix this, you should probably just need to install the gem 'rails-i18n'. It will handle returning the correct date formats for supported locales. In my case I had a custom configuration in my es.yml locale file that returned an incorrect date format.