Rails 覆盖参数值

Rails overwriting params value

我正在尝试接收有关 Trello model when a change occurs, which I'm using their webhooks 的更新。问题是参数名称之一是 "action",它似乎被 Rails 覆盖,具体取决于 Routes.rb 中的值。有什么办法可以避免这种情况,还是我只能忍受它?


Routes.rb

  match "/trello" => "trello_updates#index", via: [:get,:post]

Webhook 响应

Parameters: {"model"=>{...},"action"=>"index"}

您可以在初始化程序中编写中间件并更新来自 trello webhook 的参数。如下所示 -

class TrelloWebhooks

  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    trello_action = request.params['action']
    request.update_param('trello_action', trello_action)
    status, headers, response = @app.call(env)
    [status, headers, response]
  end

end

Rails.application.config.middleware.use 'TrelloWebhooks'

我不得不修改来自 Vishnu 的代码,这是使它与 post 请求一起工作的公认答案,所以如果你有一个 post 请求,你需要获取参数从响应正文中出来:

class TrelloWebhooks

  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    body = JSON.parse(request.body.string)
    trello_action = body["action"]
    request.update_param('trello_action', trello_action)
    status, headers, response = @app.call(env)
    [status, headers, response]
  end

end

Rails.application.config.middleware.use 'TrelloWebhooks'