Read more

How to deal with MethodNotAllowed errors

Tobias Kraze
December 09, 2011Software engineer at makandra GmbH

One of the most common production errors are ActionController::MethodNotAllowed errors. They usually happen when someone reloads a form by pressing enter/return in the URL field, or by opening JavaScript links incorrectly.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

The attached initializer provides a default way to deal with this.

You'll get the following behaviour:

  • if the incorrect request has a HTTP_REFERER coming from the same application, set a flash, and redirect back
  • if the incorrect request has no HTTP_REFERER or one coming from an external source, set a flash and redirect to the root url

Install

To use it, put the trait somewhere it can be found (app/controllers/shared) and add this to your ApplicationController

does 'handle_method_not_allowed'

You can add two options:

does 'handle_method_not_allowed', :flash => "Please try again", :fallback_url => proc { some_url }

Both can be strings or procs. The default for :flash is a friendly prompt to try again, the default for :fallback_url is root_url.

This depends on modularity Show archive.org snapshot .

Posted by Tobias Kraze to makandra dev (2011-12-09 12:06)