Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Pass the build arguments through to new_element_path. #43

Merged
merged 1 commit into from

3 participants

@seanhandley

Failing to pass the attributes to new_element_path in the self.build method leads to bad requests for nested element paths.

For example, an api with self.site set to be

'foo.com/customers/:customer_id/items/:item_id'

will generate the url

/customers//items/new.json

when Item.build is called and the API returns an error code as a result.

If we pass the attributes from self.build through to new_element_path, this behaviour is corrected and build uses the correct url to initialise the empty ActiveResource object.

/customers/1/items/new.json
@jeremy
Owner

Thanks! Could you add a test case that demonstrates the error & its fix?

@seanhandley

@jeremy I'm happy with this now if you are.

@jeremy
Owner

Thanks @seanhandley. Could you push a single squashed commit?

@seanhandley seanhandley Pass the build arguments through to new_element_path.
Failing to do this leads to bad requests for nested element paths
so an api with self.site set to be

'foo.com/customers/:customer_id/items/:item_id'

will generate the url

/customers//items/new.json

when Item.build is called and the API returns an error code.

If we pass the attributes from build to new_element_path, this
behaviour is corrected such that build calls the url:

/customers/1/items/new.json
fd3616a
@jeremy jeremy merged commit d6bbbc3 into rails:master

1 check passed

Details default The Travis build passed
@mvlwn

I ran into the same issue. Great to see this was taken care of already. Cheers.

@seanhandley

@mvlwn I'm glad - thanks for saying so!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2012
  1. @seanhandley

    Pass the build arguments through to new_element_path.

    seanhandley authored seanhandley committed
    Failing to do this leads to bad requests for nested element paths
    so an api with self.site set to be
    
    'foo.com/customers/:customer_id/items/:item_id'
    
    will generate the url
    
    /customers//items/new.json
    
    when Item.build is called and the API returns an error code.
    
    If we pass the attributes from build to new_element_path, this
    behaviour is corrected such that build calls the url:
    
    /customers/1/items/new.json
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 1 deletion.
  1. +1 −1  lib/active_resource/base.rb
  2. +14 −0 test/cases/base_test.rb
View
2  lib/active_resource/base.rb
@@ -766,7 +766,7 @@ def collection_path(prefix_options = {}, query_options = nil)
# Returns the new resource instance.
#
def build(attributes = {})
- attrs = self.format.decode(connection.get("#{new_element_path}", headers).body).merge(attributes)
+ attrs = self.format.decode(connection.get("#{new_element_path(attributes)}", headers).body)
self.new(attrs)
end
View
14 test/cases/base_test.rb
@@ -656,6 +656,20 @@ def test_build_with_custom_header
Person.headers.delete('key')
end
+ def test_build_without_attributes_for_prefix_call
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/1/addresses/new.json", {}, StreetAddress.new.to_json
+ end
+ assert_raise(ActiveResource::InvalidRequestError) { StreetAddress.build }
+ end
+
+ def test_build_with_attributes_for_prefix_call
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/1/addresses/new.json", {}, StreetAddress.new.to_json
+ end
+ assert_nothing_raised { StreetAddress.build(person_id: 1) }
+ end
+
def test_save
rick = Person.new
assert rick.save
Something went wrong with that request. Please try again.