Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9529 (new defect)

Opened 8 months ago

Last modified 5 months ago

[PATCH] ActiveResource::Base#id_from_response throws nil exception in some cases

Reported by: zippy Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveResource Version: edge
Severity: major Keywords: id_from_response tiny simple unverified
Cc:

Description

ActiveResource::Base#id_from_response needs to check to see if responseLocation? is non-nil before trying to access it because it will throw an exception instead of handling that case gracefully. Proposed change:

def id_from_response(response)

if responseLocation?

responseLocation?[/\/([\/]*?)(\.\w+)?$/, 1]

else

nil

end

end

Test cases will be forthcoming.

Attachments

id_from_response.diff (0.7 kB) - added by zippy on 09/22/07 01:24:10.
patch file to fix responses without a "Location" header

Change History

09/22/07 01:24:10 changed by zippy

  • attachment id_from_response.diff added.

patch file to fix responses without a "Location" header

09/22/07 01:29:15 changed by zippy

  • keywords changed from id_from_response to id_from_response tiny simple.

09/22/07 17:22:41 changed by david

  • keywords changed from id_from_response tiny simple to id_from_response tiny simple unverified.

When is the Location header nil? Also, please add tests to verify this behavior.

09/23/07 22:44:31 changed by david

  • summary changed from ActiveResource::Base#id_from_response throws nil exception in some cases to [PATCH] ActiveResource::Base#id_from_response throws nil exception in some cases.

09/24/07 13:53:47 changed by zippy

I think the Location header is nil whenever the reply from the server doesn't include one, which in a standard rails app seems to be any time the create request encountered an error. Thus a call to ActiveResource#create ends up sending a POST to a server, which may or may not choose to set the Location header. The standard "generate scaffold_resource" will produce something like this for the 'create' controller :

    respond_to do |format|
      if @entity.save
        flash[:notice] = 'Entity was successfully created.'
        format.html { redirect_to entity_url(@entity) }
        format.xml  { head :created, :location => entity_url(@entity) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entity.errors.to_xml }
      end
    end

Thus in the case of an error, no Location header is set. So the

  self.id = id_from_response(response)

call in ActiveResource#create will end up throwing the nil.

I'm new to the game of Tests, so it's taking me a bit of time to create a test to duplicate this, but I'm working it!

12/14/07 00:59:43 changed by mdemare

+1