Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created August 27, 2010 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shouichi/553918 to your computer and use it in GitHub Desktop.
Save shouichi/553918 to your computer and use it in GitHub Desktop.
# This rack app overrides http method by __method=xxx query.
#
# Example
#
# link_to 'Destroy', post_path(@post, :__method => :delete)
class RequestMethodOverrider
HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
HTTP_METHOD_OVERRIDE_KEY = "__method".freeze
def initialize(app)
@app = app
end
def call(env)
method = Rack::Utils.parse_query(env["QUERY_STRING"])[HTTP_METHOD_OVERRIDE_KEY].try(:upcase)
env["REQUEST_METHOD"] = method if HTTP_METHODS.include?(method)
@app.call(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment