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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)