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
A couple of years and a major version of Rails later, there doesn't seem to be any progress on this issue.
This did the job for me at the top of routes.rb
:
Rails.application.routes.draw do
def put(*) end
...
Because the set_member_mappings_for_resource
method mentioned in the OP's answer calls put
, this simply makes it a no-op.
If you do need put
routes, you could put them above this line. If you wanted to be fancy, you could define a without_verbs(*verbs, &block)
method that temporarily replaces the various verb methods, yields, and then puts them back.
another solution to override PATCH is the following:
resources :some_resources do
member { patch action: :event }
end
This will result in calling the method event
on the SomeResourceController
when we call this route PATCH /some_resources/:id
or to disable it:
resources :some_resource, except: :update do
member { put action: :update }
end
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
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.
Until then, the best workaround would be what Jorge de los Santos has suggested, although this would pretty much pollute config/routes.rb
.
Yes it is, check the docs:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
resources :photos, except: :update
PATCH and PUT are meant for different uses. As long as the update is partial you should be using PATCH but if you are updating everything you must use PUT. This can sound confusing, but for example let's suppose you are updating the associated model, this might be considered a put action instead of a patch as long as you are not modifying a partial info, you are update the whole relation.
Also PUT is used to update or create, soy might be found it useful when adding nested resources.
http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
---------------EDIT with solution:
To override puts ad:
PATCH 'route', to: 'books#update'
resources :books, except: :update
Rails will catch the patch before the resources and will disable the put and patch for update.
Reference:
Override "show" resource route in Rails