Actionpack 3.1 - number_to_human_size without defaults errors #2802

Closed
christianblais opened this Issue Sep 1, 2011 · 21 comments

Projects

None yet
@christianblais
Contributor

When using number_to_human_size without any defaults in locale.yml, I get all sorts of error :

wrong argument type nil (expected Fixnum)
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `round'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `number_with_precision'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'

[...]

can't convert NilClass to String
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:285:in `escape'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:285:in `number_with_precision'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'

Adding defaults in locale.yml solved the problem. The format use was :

number:
    format:
      precision: 3
      separator: '.'
@arunagw
Member
arunagw commented Sep 5, 2011

@christianblais I really can't reproduce the problem with new application.

Steps i did

  1. New Application with rails 3.1
  2. A scaffold to call method in Layout file
  3. number_to_human_size(123) added in layout file.

And it's displaying 123 on page.

my en.yml is

# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
  hello: "Hello world"

Is i am missing something here?

@christianblais
Contributor

@arunagw Have you tried with something that actually requires a separator or precision?

  • New application with rails 3.1
  • A scaffold to call method in Layout file
  • number_to_human_size(123.456) added in layout file

Here again, same error :

wrong argument type nil (expected Fixnum)
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `round'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `number_with_precision'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'

I'm sorry. I wasn't clear enough the first time.

@arunagw
Member
arunagw commented Sep 5, 2011

Can't reproduce again. I have uploaded my app here. Do you like to have a look?

https://github.com/testwork/issue_2802

And added here https://github.com/testwork/issue_2802/blob/master/app/views/layouts/application.html.erb#L10

And /blogs loaded fine for me.

@christianblais
Contributor

My bad. As I'm a French speaker, my locale is by default set to fr.yml. There is, indeed, no problem with your application. I just figured out that the problem occurs only when your locale isn't set to :en.

config/application.rb

config.i18n.default_locale = :fr

config/locales/fr.yml

fr:
    hello: "Bonjour"

app/views/layouts/application.html.erb

number_to_human_size(123456789.123456789)

I get the following errors, in this exact order

Error #1

translation missing: fr.number.human.storage_units.format

Error #2

translation missing: fr.number.human.storage_units.units.mb

Error #3

wrong argument type nil (expected Fixnum)

actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `round'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:281:in `number_with_precision'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'

Should have been :

translation missing: fr.number.format.precision

Errors #4

can't convert NilClass to String

actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:285:in `escape'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:285:in `number_with_precision'
actionpack (3.1.0) lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'

Should have been :

translation missing: fr.number.format.separator
@arunagw
Member
arunagw commented Sep 6, 2011

I am not sure about this behavior that you need to have these default translation in your locale file if you are not using en.yml

Is this was working with older rails version like 3.0.X ?

@asanghi
Contributor
asanghi commented Sep 6, 2011

Perhaps you need to include the rails-i18n gem which includes a number of translations for various languages include French?

@christianblais
Contributor

@arunagw I will check this out and let you know.
@asanghi If so, should there be any message telling me so?

@asanghi
Contributor
asanghi commented Sep 6, 2011

@christianblais Arun is right, you need to provide your region's locale file in your application containing all the required localizations.

Rails does not maintain all the localizations within its core. You have to include the rails-i18n gem to get various a bouquet of locale files OR you can pick the one you need and copy the locale file from that gem into your project and maintain it within your project as necessary. It rightly complains that you are missing the necessary locale entries. You are right in that it doesnt tell you what to do when you get that type of error, but I assume the Rails core team expect the developer to go thru the I18n Rails guide for necessary information at http://guides.rubyonrails.org/i18n.html

@christianblais
Contributor

@asanghi I understand I need to provide my region's locale file. My concern was more about the 3rd and 4th errors, where Rails doesn't complain about the missing entry, but instead throws an error. Shouldn't the required "fr.number.format.precision" and "fr.number.format.separator" be :raise => true, like others, so one would know that this is a missing I18n entry issue? Anyway, thank you guys for having taken the time to help me!

@asanghi
Contributor
asanghi commented Sep 6, 2011

@christianblais i think you are right! You have a wonderful opportunity to provide a patch to Rails! :)

@arunagw
Member
arunagw commented Sep 6, 2011

Hey @christianblais,

I just tried with fr.yml in the same project. I got only one here. How can i got the three error? By adding one by one elements in locale ?

ActionView::Template::Error (translation missing: fr.number.human.decimal_units.units):
    7:   <%= csrf_meta_tags %>
    8: </head>
    9: <body>
    10:   <%= number_to_human(123.456)%>
    11: 
    12: <%= yield %>
    13: 
  app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__3857138237318176486_70342817832380'
  app/controllers/blogs_controller.rb:7:in `index'

Rendered /Users/arunagw/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Users/arunagw/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/arunagw/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (4.9ms)

I have pushed my changes into same repo.

@christianblais
Contributor

@arunagw Exactly. Fixing the first error led me to the second, etc. To get the last one, with the separator, you'll need to provide a bigger number.

@partydrone

Getting the following error with number_to_human_size:

TypeError: wrong argument type nil (expected Fixnum)
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/actionpack-3.1.1/lib/action_view/helpers/number_helper.rb:281:in `round'
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/actionpack-3.1.1/lib/action_view/helpers/number_helper.rb:281:in `number_with_precision'
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/actionpack-3.1.1/lib/action_view/helpers/number_helper.rb:362:in `number_to_human_size'
    from (irb):5
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/aporter/.rvm/gems/ruby-1.9.2-p290@wavetronix/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I have included localization YAML files in my project. This seems to only occur when the locale contains a country code (i.e., :"en-GB", :"fr-CA", :"ru-KZ", etc.) It looks like this helper doesn't deal well with I18n fallbacks, but other helpers like number_to_currency seem to work just fine.

I don't know if this is a separate issue, but it seems related in that I don't have a localization file for those types of locales, so I see the error. If the locale contains only the language code (i.e., :en, :fr, :ru, etc.) the page loads without problem.

@hubb
hubb commented Dec 14, 2011

I had the same issue with number_to_human.
You can either use the rails-i18n gem or put a precision: 3 #or whatever in your locale file under number.human.format

@christianblais
Contributor

A patch was submitted in septembre, but still pending.

christianblais@8754fe4
christianblais@0e85e3f

@shingara
Contributor

Same behavior on Rails 3.2.2 to me.

@mitio
Contributor
mitio commented May 2, 2012

@christianblais, I see you've created the pull request in #2890 which should resolve this issue when merged.

I suggest that we close this issue here and move the discussion in #2890 so that we don't maintain duplicate issues. (#2890 is also an issue too, not only a pull request.)

@rafaelfranca
Member

@mitio we have to leave this issue open since if your pull request was not merged will we loose the tracking of this issue. When it was really fixed I'll close.

@pivotale20

There is actually a bug ticket on I18n for this. Not sure if you want to fix this in Railss or in I18n

svenfuchs/i18n#137

@carlosantoniodasilva carlosantoniodasilva added a commit to carlosantoniodasilva/rails that referenced this issue May 14, 2012
@carlosantoniodasilva carlosantoniodasilva Fallback to :en locale instead of handling a constant with defaults
Action Pack already comes with a default locale fine for :en, that is
always loaded. We can just fallback to this locale for defaults, if
values for the current locale cannot be found.

Closes #4420, #2802, #2890.
032142b
@schneems
Member

Looks like the convo has moved to #2890, we should close this issue in favor of that one.

@arunagw arunagw closed this Jul 21, 2012
@carlosantoniodasilva carlosantoniodasilva added a commit to carlosantoniodasilva/rails that referenced this issue Aug 11, 2012
@carlosantoniodasilva carlosantoniodasilva Fallback to :en locale instead of handling a constant with defaults
Action Pack already comes with a default locale fine for :en, that is
always loaded. We can just fallback to this locale for defaults, if
values for the current locale cannot be found.

Closes #4420, #2802, #2890.
f6b7149
@carlosantoniodasilva carlosantoniodasilva added a commit that referenced this issue Aug 11, 2012
@carlosantoniodasilva carlosantoniodasilva Fallback to :en locale instead of handling a constant with defaults
Action Pack already comes with a default locale fine for :en, that is
always loaded. We can just fallback to this locale for defaults, if
values for the current locale cannot be found.

Closes #4420, #2802, #2890.
a9dccda
@carlosantoniodasilva

It has been merged to master in 56627b6. Please let us know if you have any other issue after this. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment