Joshua Warchol's Rails Tips

Why do I get "undefined method `size' for Float" warnings?

Are you using AJAX? Maybe an in-place text editor? Or one of those fancy "click the star to rate this" jobbers? It was that latter case for me. We were using "render :text => something.overall_rating, :layout => false" where the overall_rating was a Float. Rails gave this strange error message:

A NoMethodError occurred in something#rate:

  undefined method `size' for 4.428:Float
  
  /usr/lib64/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/cgi_process.rb:228:in `set_content_length!'
  /usr/lib64/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/cgi_process.rb:187:in `out'
  /usr/lib64/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in `process'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `synchronize'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `process'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:in `process_client'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `each'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `process_client'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `initialize'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `new'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `initialize'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `new'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:271:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `each'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in `run'
  /usr/lib64/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
  /usr/bin/mongrel_rails:16:in `load'
  /usr/bin/mongrel_rails:16

So in that cgi_process.rb code, it has a variable called @body (the body of what's being rendered back to the client) and it calles #size on it to set the content length header. It expects the body to be a String, reasonably so. So the issue is that we were passing a Float to "render :text". Simply adding .to_s to the end of our float fixed the problem. Hope this saves you some time!


Joshua E. Warchol Fork me on GitHub