Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit fd3616a46b90b10015a47b1275c1109ab836e9f3 1 parent 6639bb4
@seanhandley seanhandley authored seanhandley committed
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
Please sign in to comment.
Something went wrong with that request. Please try again.