Simple respond_with in rails that avoids 204 from PUT

前端 未结 6 479
有刺的猬
有刺的猬 2020-12-23 17:46

I want to PUT to rails and avoid getting a 204. I am using this pattern:

class SomeController < ApplicationController
  respond_         


        
6条回答
  •  粉色の甜心
    2020-12-23 18:02

    As a less invasive alternative, you can pass a json: option to the respond_with method invocation inside your controller update action, like this:

    def update
      # ...
      respond_with some_object, json: some_object
    end
    

    Granted it seems a bit unDRY having to repeat the object twice in the arguments, but it'll give you what you want, the json representation of the object in the response of a PUT request, and you don't need to use the render json: way, which won't give you the benefits of responders.

    However, if you have a lot of controllers with this situation, then customizing the responders, as jpfuentes2 showed in the accepted anwser, is the way to go. But for a quick single case, this alternative may be easier.

    Source: https://github.com/plataformatec/responders/pull/115#issuecomment-72517532

提交回复
热议问题