开发者

Can ActiveResource POST a create instead of PUTTing it when an id is specified?

开发者 https://www.devze.com 2022-12-19 18:02 出处:网络
I\'m calling a rails app from another using ActiveResource. I need to supply the id of the new object to the first app (yes, controller create in this app knows how to handle receiving an id), so I do

I'm calling a rails app from another using ActiveResource. I need to supply the id of the new object to the first app (yes, controller create in this app knows how to handle receiving an id), so I do something like this:

a = ActiveResourceModel.new(:id => 1231231, :name =开发者_如何学Python> "test")
a.save

However, instead of doing POST to create a new resource it PUTs it, causing the receiving app to try to update the resource with id 1231231, which of course doesn't exist (I want to create it!), so I end up receiving a 404 error because of this.

Doing some testing the problem seems to be in ActiveResourceModel.new? which returns false, while ActiveResourceModel.exists? returns false too (Great, two methods which are supposed to be opposite return the same!).


Checking the AResource source and documentation, the new? method checks for the presence of the id and the exists? checks for the remote resource, making both returning the same.

Why exactly you need to pass the id to create a new object? Doesn't make sense. Anyway, You can try to call create method instead of save.


I have the opposite problem. I EXPECT a PUT when calling AR.create with an id (since it implies the record already exists). However, with Rails 3.1 and up, it seems like the same code in Rails 3.0 that called PUT now in fact calls POST. Can anyone confirm this change? (Since I have control of the receiving server, I simply adjusted the POST code to have the same behavior as my old PUT code).

0

精彩评论

暂无评论...
验证码 换一张
取 消