Created
September 26, 2011 22:19
-
-
Save viola/1243572 to your computer and use it in GitHub Desktop.
Render 405 response for ActionController::UnknownHttpMethod exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActionController::UnknownHttpMethod exception found under rails 3.0.1 | |
$ cat lib/http_method_not_allowed.rb | |
# Render 405 response for ActionController::UnknownHttpMethod exceptions like: | |
# (ActionController::UnknownHttpMethod) "CONNECT, accepted HTTP methods are get, head, put, post, delete, and options" | |
# (ActionController::UnknownHttpMethod) "PROPFIND, accepted HTTP methods are get, head, put, post, delete, and options" | |
class HttpMethodNotAllowed | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
# irb(main):001:0> ActionController::Request::HTTP_METHODS # => ["get", "head", "put", "post", "delete", "options"] | |
if !ActionController::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"].downcase) | |
Rails.logger.info("ActionController::UnknownHttpMethod: #{env.inspect}") | |
[405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] | |
else | |
@status, @headers, @response = @app.call(env) | |
[@status, @headers, @response] | |
end | |
end | |
end | |
Use HttpMethodNotAllowed as middleware config/application.rb | |
config.middleware.use "HttpMethodNotAllowed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works to suppress the error.
@dleve123 to suppress rack exceptions in general, set
RACK_ENV
todeployment
.