Reputation: 6639
Rails 5.2.2
In my routes.rb file, I have:
resources :users, only: [:index, :new, :create, :update] do
put 'disable', on: :member
end
When I do rake routes, I get:
disable_user PUT /users/:id/disable(.:format) users#disable
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
Why do I have a duplicate of the update route? or this just a visual artifact of the rake route command?
Upvotes: 2
Views: 28
Reputation: 102213
Rails originally only used the PUT http verb for updates. In 2012 PATCH was declared the new primary HTTP method for updates because of the semantics of how IETF defines PUT.
Now let’s say a web application has an Invoice model with a paid flag that indicates whether the invoice has been paid. How do you set that flag in a RESTful way? Submitting paid=1 via PUT to /invoices/:id does not conform to HTTP semantics, because such request would not be sending a complete representation of the invoice for replacement.
The entire discussion on the hows and why's can be found in issue #348.
However for legacy compatibility reasons the resources
and resource
macros still generate a PUT route. This was a better compromise than breaking the Rails router to treat PUT and PATCH identically.
Why this legacy compatibility still is here in 2019 is another really good question.
Upvotes: 2