Simple respond_with in rails that avoids 204 from PUT

前端 未结 6 482
有刺的猬
有刺的猬 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:09

    Just to clarify, you do not need the responders gem to do this... You can just do:

    config/initializers/responder_with_put_content.rb

    class ResponderWithPutContent < ActionController::Responder
      def api_behavior(*args, &block)
        if put?
          display resource, :status => :ok
        else
          super
        end
      end
    end
    

    and then either (for all updates actions to be affected):

    class ApplicationController < ActionController::Base
      def self.responder
        ResponderWithPutContent
      end
    end
    

    or in your action:

    def update
      foo = Foo.find(params[:id])
      foo.update_attributes(params[:foo])
      respond_with foo, responder: ResponderWithPutContent
    end
    

提交回复
热议问题