interlude

プログラミングや私的なこと

sinatraでPUTメソッドを実装したい

解決策 enable :method_overrideを記述すること

sinatraのアップデートメソッドを実装しようとしたら、なかなか上手くかなかった。 (エラーメッセージは記録するの忘れた。後悔。。) そこで調べてみたところ、以下の記事を発見!

tomiacannondale.hatenablog.com

具体的な方法は分かったけど、何故?というところが詳しく書かれていなかったので sinatraのドキュメントを読んでみることに。

Sinatra: Configuring Settings

:method_override - enable/disable the POST _method hack

という項目を読んでみると、

Boolean specifying whether the HTTP POST method parameter hack should be enabled. When true, the actual HTTP request method is overridden by the value of the method parameter included in the POST body. The _method hack is used to make POST requests look like other request methods (e.g., PUT, DELETE) and is typically only needed in shitty environments – like HTML form submission – that do not support the full range of HTTP methods.

The POST _method hack is implemented by inserting the Rack::MethodOverride component into the middleware pipeline.

と書いてある。 enableにしてPOSTするとPutやDeleteのhttpリクエストが送れるということと理解した。 ただ仕組みまではちょっと調べる気になれなかった。

やり方は分かったけど、構造については分からないのがちょっとモヤっとしますね。。。