How to deal with MethodNotAllowed errors

Posted Over 12 years ago. Visible to the public.

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.

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 .

Tobias Kraze
Last edit
About 12 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2011-12-09 11:06)