Here is a miniature proof of concept. It fetches thing #100 and then saves it.
var things = ThingsAPI.all("things");
things.one(100).get()
.then(function(thing) {
thing.put();
})
First it makes a GET request to
http://localhost:8080/things/100
but after that it PUTs to
http://localhost:8080/things/100/100
I would expect it to PUT to the same URL it came from, instead it's treating the url http://localhost:8080/things/100
as a list and then trying to find entity 100
within it.
I just want this to PUT back to http://localhost:8080/things/100
. What am I doing wrong?
EDIT:
If I fetch with this instead, the PUT
ting works. But I would have expected to be able to do this with the all
method.
ThingsAPI.one('things', 100).get()
And, for clarity, here is where I define ThingsAPI
.
app.factory('ThingsAPI', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost:8080/');
});
});
The reason is that you are treating the 100
as a nested resource for things
. Therefore Restangular will generate routes as if http://localhost:8080/things/100
is the index route for the resource named 100
-- although it actually is the show route for things
resource with id 100
. These routes just accidentally happen to have the same URI.
As you can see, Restangular has misunderstood your intented resource definiton, and eg. PUT
will generate an incorrect route.
Treating 100
as an identifier for things
resource will correct the issue, as you seem to have found out: ThingsAPI.one('things', 100).get()
This translates to one(resource, identifier).get()
.