Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

respond_with should exhibit common behaviour for "API" requests, even when a template exists.

#4579

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

Reported by Lawrence Pit · May 12th, 2010 @ 04:04 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Lawrence Pit
    Lawrence Pit

    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 ?

    May 13th, 2010 @ 02:38 AM

  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to needs-more-info

    Destroy wouldn't because that resource no longer exists. Please show how to reproduce this bug.

    May 13th, 2010 @ 11:42 PM

  3. Lawrence Pit
    Lawrence Pit

    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?

    May 19th, 2010 @ 07:05 AM

  4. Evgeniy Dolzhenko
    Evgeniy Dolzhenko
    • 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...

    August 17th, 2010 @ 06:08 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:00 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:00 PM