Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Error with date_select when a default_locale is set

#1389

After adding

I18n.default_locale = 'ja-JP'

in my environment.rb, I got the following error:

can't convert Symbol into String

when using

<%= f.date_select :contract_from %>

in my view.

The trace is:

/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/date_helper.rb:564:in `include?'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/date_helper.rb:564:in `select_date'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/date_helper.rb:832:in `to_date_select_tag_without_error_wrapping'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/active_record_helper.rb:268:in `to_date_select_tag'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/date_helper.rb:179:in `date_select'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.1/lib/action_view/helpers/date_helper.rb:889:in `date_select'

If I install the localized_date plugin, I get a proper error in the console, but the "can't convert Symbol into String" of standard Rails does not mean anything for the normal user.

Reported by Vincent Isambart · November 17th, 2008 @ 06:17 AM

State: stale
Milestone: 3.x
Assigned to: Sven Fuchs Sven Fuchs
Importance: none

Activity

  1. Vincent Isambart
    Vincent Isambart

    I investigated a little more and there is at least one problem with ActionView::Helpers::DateTimeSelector#translated_date_order. The current code is

        def translated_date_order
          begin
            I18n.translate(:'date.order', :locale => @options[:locale]) || []
          end
        end
    
    

    However, I18n.translate never returns nil/false. If it did not find anything, it will return a "translation missing: xxxx" string. And later calling .include(:symbol) on it will throw an exception. At first I thought about adding :default => [] in the translate()'s options, but the only defaults that can be used are strings (if you give an Array, it looks for the strings in it). To return an empty array if nothing was found you would have to do something like this:

        def translated_date_order
          begin
            order = I18n.translate(:'date.order', :locale => @options[:locale])
            if order.respond_to?(:to_ary)
              order
            else
              []
            end
          end
        end
    
    

    I tried it and date_select displayed "Some date". A nice error would be better but it's still better than a strange "can't convert Symbol into String" exception.

    November 22nd, 2008 @ 03:39 AM

  2. Josh Delsman
    Josh Delsman

    Yep, I am having the same problem. I have the default set to "en-US", and I get the same Can't convert Symbol to String error.

    November 25th, 2008 @ 06:19 PM

  3. n3uro5i5
    n3uro5i5

    I used your code, but i changed the empty array to a default date order.

    module ActionView module Helpers

    class DateTimeSelector
      def translated_date_order
        begin
          order = I18n.translate(:'date.order', :locale => @options[:locale])
          if order.respond_to?(:to_ary)
            order
          else
            [:year, :month, :day]
          end
        end
      end
    end
    
    

    end end

    November 28th, 2008 @ 11:35 AM

  4. bruno
    bruno

    Same problem using default_locale= 'en-US'

    December 9th, 2008 @ 06:29 PM

  5. Dwayne Macgowan
    Dwayne Macgowan

    I have the same problema.

    While the problem is solved what you can do is explicit your :order every time you call date_select. This will avoid the error.

    December 17th, 2008 @ 03:05 AM

  6. Mr.Devin
    Mr.Devin

    I had the same problem but found that since this is the datetime selector you need to add :time in your default order. this might be obvious to other but i am a bit dense so it is good to make not of this for other who are like me. :)

    @@@RUBY class DateTimeSelector def translated_date_order

    begin
      order = I18n.translate(:'date.order', :locale => @options[:locale])
      if order.respond_to?(:to_ary)
        order
      else
        [:year, :month, :day, :time]
      end
    end
    
    

    end end

    
    

    December 17th, 2008 @ 01:25 PM

  7. Dwayne Macgowan
    Dwayne Macgowan

    I found a better solution. You need to define your localization for time formats. You can get some pre-made localization files for many languages at: http://github.com/svenfuchs/rail...

    December 17th, 2008 @ 01:58 PM

  8. Yaroslav Markin
    Yaroslav Markin

    @bruno :'en-US' is not the default locale anymore, did you try :en?

    December 26th, 2008 @ 10:41 AM

  9. DavidBackeus
    DavidBackeus

    I solved it by adding a en-US.yml with the formats on this page: http://i18n-demo.phusion.nl/page.... Would be nice if the helpers would be fixed to use rails defaults if no specifics are found.

    January 21st, 2009 @ 04:39 PM

  10. Ryan Bigg
    Ryan Bigg

    DavidBackeus, could you submit a patch for this? Thanks!

    April 10th, 2010 @ 11:33 AM

  11. Ryan Bigg
    Ryan Bigg
    • Assigned user set to Sven Fuchs

    I have asked Sven Fuchs to take a look at this and give his opinion.

    April 10th, 2010 @ 11:38 AM

  12. Sven Fuchs
    Sven Fuchs
    • State changed from new to open

    Thanks for reporting this.

    I guess the implementation is just wrong. It should pass the :raise => true option (so I18n.t would raise a MissingTranslationException) and then rescue from the exception.

    Also, it should help to add the locale fallbacks module here which is included in the I18n gem but it apparently is not obvious how to use it. Pratik mentioned that he wanted to enable it by default which should solve the problem with translation data missing for locales other than :en.

    http://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/fallb...

    April 10th, 2010 @ 12:13 PM

  13. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  14. Juice
    Juice

    oh, this is about 2 years old bug, I just got it.

    May 13th, 2010 @ 03:03 PM

  15. Draiken
    Draiken
    • Importance changed from to

    Is this gonna get fixed or should I manually include the fallbacks on my projects?

    October 8th, 2010 @ 03:16 PM

  16. Jean-Baptiste Escoyez
    Jean-Baptiste Escoyez

    Same problem in rails 3.0.1 ruby 1.9.2

    November 7th, 2010 @ 05:48 PM

  17. Mike Ferrier
  18. rails
    rails
    • Tag changed from 2.2, date, i18n to 22, date, i18n

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 14th, 2011 @ 12:00 AM

  19. rails
    rails
    • State changed from open to stale

    March 14th, 2011 @ 12:00 AM