【问题标题】:How to override update path to patch instead of put in activerresource rails?如何覆盖补丁的更新路径而不是放入activerresource rails?
【发布时间】:2015-05-06 22:56:18
【问题描述】:

我正在使用 activeresource 模型,我需要使用仅支持补丁请求方法调用来更新资源的 API。如何覆盖 Active 资源模型中的更新调用? 请指教

【问题讨论】:

  • 你能展示你到目前为止所做的事情吗?
  • @tversteeg: 在模型'def update connection.patch(element_path(prefix_options), encode, self.class.headers)中添加了以下方法。tap do |response| load_attributes_from_response(response) end end'
  • 本题与ActiveRestClient无关...
  • @AndyJeffries 感谢您的指出!
  • @AndyJeffries 确实与 ActiveRESTClient 有关。建议的解决方案如下。

标签: ruby activeresource ruby-on-rails-4.2


【解决方案1】:

我自己想出来的。

以下代码可以解决问题。发送那些应该只更新的参数。从请求中删除了所有其他参数。

  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

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 2020-02-06
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多