Rails覆盖params值

时间:2015-10-06 13:04:14

标签: ruby-on-rails trello

当我发生更改时,我尝试在Trello模型上接收更新,我正在使用他们的webhooks。问题是参数的一个名称是" action",它似乎被Rails覆盖,具体取决于Routes.rb中的值。有什么方法可以避免这种情况,还是只需要忍受它?

的routes.rb

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

Webhook响应

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

2 个答案:

答案 0 :(得分:2)

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

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'

答案 1 :(得分:1)

我必须修改Vishnu中的代码,这是使其适用于帖子请求的公认答案,因此如果您有帖子请求,则需要从响应正文中取出参数:< / p>

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'