Rack::MethodOverride and you

Christian Iverson
2 min readApr 30, 2021

As I was in the process of building my most recent project for FlatIron, I came across the need for some added functionality beyond what is allowed by default. Without any extensions or middleware you get two types of requests, GET and POST. Those are very useful and actually essential for websites to function properly, however they aren’t going to be enough to provide the types of services most people expect from modern web applications. With just GET and POST there’s no simple way to edit or delete things like comments, blogs or posts on social media. To deal with this most web frameworks have ways of faking other types of requests. Since I have been using Sinatra for my project I’ll be using the example I used personally, Rack::MethodOverride.

The way MethodOverride works is by using a tag that tells the application that its receiving a PATCH or DELETE request, even though its actually just a POST.

<input type=”hidden” id=”hidden” name=”_method” value=”patch”>

This line is added to forms where a PATCH request is needed. Its hidden from the user, and its name is set to “_method” so the application will know what to do with it. We just have to tell our application to expect it ahead of time with this line.

use Rack::MethodOverride

As long as this line of code is added to our config.ru file above our controllers our app will have everything it needs to use PATCH and DELETE requests. This allows much more flexibility in the kinds of web apps you can create and what you can allow your users to do.

--

--