This project is archived and is in readonly mode.
respond_with should exhibit common behaviour for "API" requests, even when a template exists.
Reported by Lawrence Pit | May 12th, 2010 @ 04:04 AM
Using rails 3 master. My +create+ controller method ends with respond_with(@user), in which case it will result in a http status code of 201 (:created) and will have :location set. However, when I define a view using xml builder, then the http status code will be 200 and no :location is set.
Ending with respond_with(@user, :status => :created) does not set the status to :created either.
Furthermore, if the user in example below couldn't be created, then the template is rendered for the @user object (which contains errors), and the http status code is 200. Here too, I would expect the common API behavior where it basically does: display resource.errors, :status => :unprocessable_entity
respond_to :xml, :json
# POST /user.<format>
def create
@user = User.create(params[:user])
respond_with(@user)
end
Comments and changes to this ticket
-
Lawrence Pit May 13th, 2010 @ 02:38 AM
Actually, why does the +create+ method respond with the resource, while +update+ (and +delete+) don't? If +create+ were to respond with an empty body and status :created and :location set, that would solve my problem. Any objections to modifying this in rails 3 ?
-
Ryan Bigg May 13th, 2010 @ 11:42 PM
- State changed from new to needs-more-info
Destroy wouldn't because that resource no longer exists. Please show how to reproduce this bug.
-
Lawrence Pit May 19th, 2010 @ 07:05 AM
mime_respond_test.rb has this test:
def test_using_resource_for_post_with_xml_yields_created_on_success with_test_route_set do @request.accept = "application/xml" post :using_resource assert_equal "application/xml", @response.content_type assert_equal 201, @response.status assert_equal "<name>david</name>", @response.body assert_equal "http://www.example.com/customers/13", @response.location end end
Which shows that when you create something it will respond with the resource in xml format in it's response body.
But in almost all my cases this default response shows a lot of stuff I don't want to expose.
So for me the solution would be to NOT respond with the resource, just like +update+ which doesn't do that either.
Alternatively if that's not the behaviour we want, then if I define a create.xml.builder file to control what gets exposed, then it should use that to build the xml on a successful create, and respond with status code 201. However, it responds with status code 200. The attached test shows this.
Furthermore, when the creation is not successful it should respond with the default API behavior as defined in the method +api_behavior+ in +responder.rb+. The attached test shows this issue too.
Which solution should we go for? As said, my preference would be that on a +post+ it will return with an empty response body always, just like with a +put+. However, this probably breaks backwards compatibility. Can we do that for rails 3.x, as that breaks a lot compared to 2.x anyways?
Last thought: is the reason to respond with the resource after a create that consuming clients might parse it to find e.g. the ID?
-
Evgeniy Dolzhenko August 17th, 2010 @ 06:08 PM
- Importance changed from to Low
+1 for "Ending with respond_with(@user, :status => :created) does not set the status to :created either." (I'm on RC)
While docs state that it should be possible http://github.com/rails/rails/blob/master/actionpack/lib/action_con...
-
Santiago Pastorino February 2nd, 2011 @ 05:00 PM
- State changed from needs-more-info to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 05:00 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>