【问题标题】:PUT or PATCH `_method` param not working or not being respected in rails-apiPUT 或 PATCH `_method` 参数在 rails-api 中不起作用或不受尊重
【发布时间】:2018-08-15 12:00:52
【问题描述】:

TLDR:

使用rails --api,预期的rails从POST and params[:_method]='put'路由为PUT方法,但路由为POST

给定:

  • 导轨 4.2.8
  • rails-api 0.4.1

考虑以下几点:

config/routes.rb:

resources :sessions, do
  put 'authenticate', on: :collection
end

一些客户端 HTML 文件:

<form action='http://localhost:3000/sessions/authenticate' method='post'>
  <input type='hidden' name='_method' value='put'>
  ...
</form>

...提交表单时:

rails server 输出:

Started POST "/sessions/authenticate" for ::1 at 2018-03-07 11:20:21 +0000
No route matches [POST] "/sessions/authenticate" excluded from capture: DSN not set

ActionController::RoutingError (No route matches [POST] "/sessions/authenticate"):
  actionpack (4.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.2.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  ...

Inspecting Chrome -> Developer Tools -> Network tab -> [REQUEST],payload正确如下图:

常规
请求网址:http://localhost:3000/sessions/authenticate
...

表单数据
_方法:放置
...

疑难解答

  • 我尝试在一个新的 Rails 项目中复制确切的代码,但这次没有使用--api 模式,并且请求有效:它正确识别了_method='PUT' 并路由为PUT。所以,我有一种感觉,这与普通 rails 和 api 模式之间的一些差异有关。不幸的是,经过多次搜索,我找不到任何解决方案。

任何帮助将不胜感激。

【问题讨论】:

  • 你使用什么 gem/package/... 来查询你的端点?
  • @Ben 我刚刚更新了我的问题以包括rails-api 0.4.1。但是,如果您在谈论“发布”本身的表单,我没有使用任何东西,它只是一个简单的.html 文件。
  • 好的,我明白了;显然答案成功了;之所以问,是因为我倾向于使用 axios 或相关工具查询 rails 端点;只需指定 PUT 或 PATCH 方法,它们就可以像魅力一样工作
  • @Ben 哦,我看到像postman?是的,指定 PUT 作为 HTTP 方法在 Postman 中有效,但指定 POST_method='put' 在 Postman 中无效。但是谢谢你的帮助!!! :)
  • 是的,sergio 确实提供了一个准确的解决方案;这里需要 rails_api (:

标签: ruby-on-rails rails-api


【解决方案1】:

_method 隐藏字段的处理由一个机架中间件完成。

http://guides.rubyonrails.org/configuring.html#configuring-middleware

如果设置了params[:_method]

Rack::MethodOverride 允许覆盖该方法。这是支持 PATCH、PUT 和 DELETE HTTP 方法类型的中间件。

我猜你在 api 模式下没有那个。添加它,请求应该被正确路由。

奖励曲目

我自己不知道(或者很久以前忘记了)。我是这样发现的。

  1. 转到https://github.com/rails/rails 并尝试在该repo 中搜索:_method(因为这很可能是rails 处理该字段的方式)。两分钟后发现 github 没有搜索确切的术语。
  2. 在本地克隆 rails repo(5 分钟)
  3. Grep 本地代码库(0.5 秒)

    sergio@soviet-russia ‹ master › : ~/projects/github/rails
    [0] % ag ":_method"
    guides/source/rails_on_rack.md
    238:* Allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PUT and DELETE HTTP method types.
    
    guides/source/configuring.md
    235:* `Rack::MethodOverride` allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PATCH, PUT, and DELETE HTTP method types.
    

总时间:约 7 分钟。 Rails 本身实际上并不包含实际的相关代码,但它有文档。我很幸运。 :)

【讨论】:

  • 成功了!!!!已经卡在这个上面2个小时了!谢谢!!! :)
  • 啊,感谢您提供详细的故障排除步骤!我现在也开始这样做! :D
  • 谢谢,谢谢,再次感谢。让我的引擎在 Rails API 中运行并遇到了这个问题。将config.middleware.use(Rack::MethodOverride) 添加到我的engine.rb 并使用补丁提交表单。
猜你喜欢
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多