Is it possible to disable the standard PUT route in Rails 4?

前端 未结 4 1356
不思量自难忘°
不思量自难忘° 2021-02-20 06:03

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

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 07:02

    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

提交回复
热议问题