Default responder doesn't honor the overwritten response block #4796

Closed
sikachu opened this Issue Jan 31, 2012 · 5 comments

Projects

None yet

3 participants

@sikachu
Ruby on Rails member

Consider this snippet:

def create
  @user = User.create(params[:user])
  respond_with(@user) do |format|
    format.json do
      if @user.errors.blank?
        render :json => @user
      else
        render :json => @user.errors
      end
    end
  end
end

The given block will get passed into the responder as a :default_response(https://github.com/rails/rails/blob/v3.2.1/actionpack/lib/action_controller/metal/mime_responds.rb#L231-240) Hoever, it will not get called due to the fact that to_format does not honor the :default_response at all (https://github.com/rails/rails/blob/v3.2.1/actionpack/lib/action_controller/metal/responder.rb#L174-182). I think we should fix this to allow the override.

Setting this to 3.2.2, and I will work on this.

@sikachu sikachu was assigned Jan 31, 2012
@spastorino
Ruby on Rails member

@sikachu can you explain better what doesn't work here?. I've just tested and works fine for me

@josevalim
Ruby on Rails member

@sikachu if you are passing the json block, in theory the responder won't even be invoked for json requests.

@josevalim
Ruby on Rails member

Hrm, I am wrong. Yeah, we should probably fix this in a way the responder won't even be invoked.

@sikachu
Ruby on Rails member

@spastorino it should not work for you. I have a failing test case which I'm going to include with my patch.

@josevalim yep, we have this problem ourselves where responder just ignore our overridden block. We want to return the errors without the root "errors" object, so this is annoying.

@spastorino
Ruby on Rails member

Ahh I see it depends if the resource has errors or not

@sikachu sikachu added a commit to sikachu/rails that referenced this issue Feb 3, 2012
@sikachu sikachu Fix override API response bug in respond_with
Default responder was only using the given respond block when user
requested for HTML format, or JSON/XML format with valid resource. This
fix the responder so that it will use the given block regardless of the
validity of the resource. Note that in this case you'll have to check
for object's validity by yourself in the controller.

Fixes #4796
3def1c8
@sikachu sikachu added a commit to sikachu/rails that referenced this issue Feb 3, 2012
@sikachu sikachu Fix override API response bug in respond_with
Default responder was only using the given respond block when user
requested for HTML format, or JSON/XML format with valid resource. This
fix the responder so that it will use the given block regardless of the
validity of the resource. Note that in this case you'll have to check
for object's validity by yourself in the controller.

Fixes #4796
567ac65
@sikachu sikachu closed this Feb 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment