是否可以禁用 Rails 4 中的标准 PUT 路由? [英] Is it possible to disable the standard PUT route in Rails 4?
问题描述
Rails 4 引入了 PATCH
请求作为对对象进行(常见)部分更新时的默认请求方法.这符合 HTTP 标准,可以在此处找到讨论此决定的好(较旧)帖子:
Rails 4 has introduced PATCH
requests to be the default request method when doing the (common) partial updates on objects. This is conformal to HTTP standards and a good (older) post discussing this decision can be found here:
http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
当你在 config/routes.rb
中定义一个资源时,比如
When you define a resource in config/routes.rb
like
resources :books
然后在 rails 4 中默认创建以下路由:
then the following routes are created per default in rails 4:
GET /books books#index
GET /books/:id books#show
POST /books books#create
DELETE /books/:id books#destroy
PATCH /books/:id books#update
PUT /books/:id books#update
因为我正在开发一个新的应用程序并且不需要关心向后兼容性,所以我想删除过时的 PUT
路由.
As I am developing a new application and do not have to care about backwards compatibility, I would like to remove the obsolete PUT
route.
在 config/routes.rb
中是否有一种简单的方法可以完成此操作?
Is there an easy way for accomplishing this in config/routes.rb
?
解释为什么这个 PUT
路线困扰我:我正在使用 swagger-docs gem 用于为我的 API 自动生成 swagger 文档.由于所描述的行为,对于每个资源的更新请求(PUT
和 PATCH
),我总是有两个端点定义.此外,由于这是一个可能会弃用的路由,我希望我的 API 从今天起不再支持它.
Explanation why this PUT
route bothers me: I am using the swagger-docs gem for automatically generating a swagger documentation for my API. Due to the described behavior, I always have two endpoint definitions for update requests (PUT
and PATCH
) for every resource. Plus as this is a potentially deprecating route, I would like my API to not support it from today on.
UPDATE 由于第一个答案朝着错误的方向前进,我想澄清一下:我不想删除更新"操作,而只想删除过时的 PUT 路由,同时保留 PATCH 路由.
UPDATE due to the first answers heading in the wrong direction I would like to clarify: I do not want to remove the 'update' action, but only the obsolete PUT route while keeping the PATCH route.
推荐答案
回答我自己的问题:不,目前无法在 rails 4 中禁用 PUT/PATCH 组合的默认生成,可以清楚地看到在 https://github 上查看 ActionDispatch::Routing 的来源时.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb 尤其是那些行:
To answer my own question: no, it is currently not possible to disable the default generation of the PUT/PATCH combination in rails 4, which can be clearly seen when looking at the source of ActionDispatch::Routing at https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb and especially those lines:
def set_member_mappings_for_resource
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
if parent_resource.actions.include?(:update)
patch :update
put :update
end
delete :destroy if parent_resource.actions.include?(:destroy)
end
end
显然,(目前)没有排除PUT
路由的条件.我会为此准备一个问题或拉取请求,稍后再回来.
Obviously, there is (currently) no conditional for excluding the PUT
route. I will prepare an issue or pull request for this and come back later with the result of that.
在那之前,最好的解决方法是 Jorge de los Santos 所建议的,尽管这会严重污染 config/routes.rb
.
Until then, the best workaround would be what Jorge de los Santos has suggested, although this would pretty much pollute config/routes.rb
.
这篇关于是否可以禁用 Rails 4 中的标准 PUT 路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!