Search code examples
rubyactiveresourceruby-on-rails-4.2

How to override update path to patch instead of put in activerresource rails?


I am using activeresource model, I need to consume API which supports only patch request method call for updating resource. How to override the update call in Active resource model? Please advise


Solution

  • Figured it out myself.

    Following code did the trick. Sent those parameters which are supposed to be updated only. Removed all other parameters from request.

      def update
        data = JSON.parse(encode)
        data_to_send = data.select{|k,v| ["name", "surname"].include?(k)}
        run_callbacks :update do
          connection.patch(element_path(prefix_options), data_to_send.to_json, self.class.headers).tap do |response|
            load_attributes_from_response(response)
          end
        end
      end