PatchPatch

PatchPatch changes Rails’ default behavior of mapping PUT and PATCH requests on resources to the same action.

Installation

Add this line to your application's Gemfile:

gem 'patch-patch'

And then execute:

$ bundle

Usage

PatchPatch hooks itself automatically into Rails’ routing engine. You don’t have to enable or configure it.

Let’s say we have a RESTful API with a single users resource.

# config/routes.rb
Foo::Application.routes.draw do
  resources :users
end

Before

$ rake routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy

After

$ rake routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user PATCH  /users/:id(.:format)      users#partial_update
          GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy

Note: the old PATCH route is still there but it will never be matched since our #partial_update route comes first. The old PUT route will still route to #update though.

License

PatchPatch is © 2013 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for iPhone, iPad, Android, Blackberry, Windows Phone and Windows 8 in beautiful Quebec City.

We also love open-source software and we try to extract as much code as possible from our projects to give back to the community.