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.

ActiveResource not handling updates correctly

#5038

Docs say that calls to PUT should return an HTTP 204 status with no content. If you follow that then you get an error in ActiveRecord::Base.load_attributes_from_response. "NoMethodError: undefined method strip' for nil:NilClass".

If you use Sinatra and write an API that conforms to the ActiveResource docs, then you should return a 204 status with no body there is no Content-Length header set...and Rack won't let you set one. 'Rack::Lint::LintError: Content-Length header found in 204 response, not allowed'

class Task < ActiveResource::Base
end
t = Task.create(:desc => "do something")
t.desc = "Do the dishes"
t.save!  <-- Errors out with 'NoMethodError: undefined method `strip' for nil:NilClass' because there is no content length header.

The fix is simple.

def load_attributes_from_response(response)
  if !response['Conent-Length'].blank? && response['Content-Length'] != "0" &&
        response.body.strip.size > 0
    load(self.class.format.decode(response.body))
  end
end

For Reference:

http://github.com/rack/rack/blob/master/lib/rack/lint.rb

Line 464: check_content_type

There must be a Content-Type, except when the
Status is 1xx, 204 or 304, in which case there must be none
given.

Reported by Mike Abner · July 2nd, 2010 @ 11:08 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Mike Abner
    Mike Abner
    • Tag changed from undefined method 'strip, activeresource, load_attributes_from_response, update to undefined method 'strip, activeresource, load_attributes_from_response, patch, update

    July 7th, 2010 @ 10:28 PM

  2. José Valim
    José Valim
    • Assigned user set to José Valim
    • Importance changed from to Low

    July 9th, 2010 @ 06:13 PM

  3. José Valim
    José Valim

    Could you please add a test to your patch to ensure we won't have regressions in the future? Thanks!

    July 13th, 2010 @ 07:11 AM

  4. José Valim
    José Valim
    • State changed from new to incomplete

    Mike, any news?

    September 2nd, 2010 @ 10:59 AM

  5. Neeraj Singh
    Neeraj Singh
    • State changed from incomplete to open

    Attached is a failing test. I mean it fails without the patch :-)

    September 8th, 2010 @ 06:58 PM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [154081f0f74988bdb8979f0447ff5816ab83d8fd]) make sure a Content-Length header exists and that the response.body is not nil before trying to call methods on it. Rack does not allow HTTP 204 responses to have a content-length header set. [#5038 ActiveResource not handling updates correctly state:resolved] http://github.com/rails/rails/commit/154081f0f74988bdb8979f0447ff58...

    September 18th, 2010 @ 07:50 PM