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.

number_to_currency fails with "NoMethodError: undefined method `>=' for nil:NilClass"

#4604

The title says it pretty much.
I use rvm, in this case ruby-1.8.7-p249. Seems to me like a ruby problem.
Output from Rails console:

Loading development environment (Rails 3.0.0.beta3)
ruby-1.8.7-p249 > foo = ActionView::Base.new
ruby-1.8.7-p249 > foo.extend ActionView::Helpers::NumberHelper
ruby-1.8.7-p249 > foo.number_to_currency(101.00)
NoMethodError: undefined method `>=' for nil:NilClass
    from /Users/fabianschwahn/.rvm/gems/ruby-1.8.7-p249@bluemoody-rails3/gems/activesupport-3.0.0.beta3/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /Users/fabianschwahn/.rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/rational.rb:526:in `**'
    from /Users/fabianschwahn/.rvm/gems/ruby-1.8.7-p249@bluemoody-rails3/gems/actionpack-3.0.0.beta3/lib/action_view/helpers/number_helper.rb:285:in `number_with_precision'
    from /Users/fabianschwahn/.rvm/gems/ruby-1.8.7-p249@bluemoody-rails3/gems/actionpack-3.0.0.beta3/lib/action_view/helpers/number_helper.rb:115:in `number_to_currency'
    from (irb):3

Reported by Fabian Schwahn · May 15th, 2010 @ 04:55 PM

State: committed
Milestone: 3.0.2
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Carlos Antonio da Silva
    Carlos Antonio da Silva
    • Tag changed from rails 3.0.0.beta3 to rails 3.0.0.beta3, action_view, number_helper

    Just tested here following the same steps, using both Ruby 1.8.7-p249 and 1.9.2-head with Rails 3.0.0.beta3 in a fresh app, nothing wrong.

    ruby-1.8.7-p249 > foo.number_to_currency(101.00)
     => "$101.00"
    

    May 15th, 2010 @ 05:06 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • Tag changed from rails 3.0.0.beta3, action_view, number_helper to rails 3.0.0.beta3, action_view, bugmash, number_helper

    May 16th, 2010 @ 12:11 AM

  3. Santiago Pastorino
    Santiago Pastorino

    Fabian, can you try it using the latest Rails version from git?.
    Thanks

    May 16th, 2010 @ 12:12 AM

  4. Enrico Bianco
    Enrico Bianco

    not reproducible in the latest master:

    ruby-1.8.7-p249 > foo.number_to_currency(101.00)
     => "$101.00"
    
    ruby-1.9.1-p378 > foo.number_to_currency(101.00)
    => "$101.00"
    
    ruby-1.9.2-head > foo.number_to_currency(101.00)
     => "$101.00"
    

    May 16th, 2010 @ 12:42 AM

  5. Fabian Schwahn
    Fabian Schwahn

    ok, I investigated a little further, and the bug only occurs if the default locale is set to :de .
    So, if I set


    config.i18n.default_locale = :de


    in config/application.rb and execute the following in the console:


    foo = ActionView::Base.new
    foo.extend ActionView::Helpers::NumberHelper
    foo.number_to_currency(101.00)


    I am able to reproduce the bug with a fresh app.

    Also happens with edge rails.

    May 16th, 2010 @ 12:42 AM

  6. Fabian Schwahn
    Fabian Schwahn

    ok, some more information:
    Apparently, if you set a locale different than :en, the number helpers stop working.
    That is because the options are set as follows (in action_view/helpers/number_helper.rb):

            defaults  = I18n.translate(:'number.format', :locale => options[:locale], :default => {})

        currency  = I18n.translate(:'number.currency.format', :locale => options[:locale], :default => {})
        defaults  = defaults.merge(currency)
    
        options = options.reverse_merge(defaults)
    
        unit      = options.delete(:unit)
        format    = options.delete(:format)
    
        begin
          value = number_with_precision(number, options.merge(:raise => true))
          format.gsub(/%n/, value).gsub(/%u/, unit).html_safe</code>
    
    
    
    
    So if I change the locale to anything else than the standard, and don't set the number formats up properly in config/locales/foo.yml, the number helpers crash, because unit, format and precision (in number_with_precision) are nil.
    A solution would be to set the :en-locale as default here:
    defaults  = I18n.translate(:'number.format', :locale => options[:locale], :default => {})
    
    in the default-hash.

    May 16th, 2010 @ 01:28 AM

  7. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to Santiago Pastorino

    Reproduced, working on a fix

    May 16th, 2010 @ 02:09 AM

  8. Santiago Pastorino
  9. Santiago Pastorino
  10. José Valim
    José Valim

    Pastorino, this still does not solve the problem. It should be something like this:

    defaults  = default_currency_values.merge(defaults).merge!(currency)
    

    May 16th, 2010 @ 10:04 PM

  11. Rafael Mendonça França
    Rafael Mendonça França

    +1

    This work fines for me. All tests pass.

    May 17th, 2010 @ 01:02 AM

  12. Santiago Pastorino
  13. Repository
    Repository
    • State changed from open to committed

    May 17th, 2010 @ 04:40 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM