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

前端 未结 4 1382
不思量自难忘°
不思量自难忘° 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 06:44

    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.

    0 讨论(0)
  • 2021-02-20 06:54

    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
    
    0 讨论(0)
  • 2021-02-20 06:55

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题