diary.sorah.jp

render_to_string が ActionController::Live と相性がわるかった

http://blog.sorah.jp/2013/07/28/render_to_string-in-ac-live

render_to_string を ActionController::Live なアクション内で利用したときに、render_to_string 以降で response.stream.write すると IOError が発生してた。

なんか response.stream が別の object (id) になってる上に閉じられてるナンデ! という話。

しらべたっていうか実装読んで驚いたのだけど render_to_string は response.body を一時的に空文字列にして render で body にかきこませた上で戻してるようだった。いろいろ事情があるんだろうなあ。

まあそれはさておき、render するときに response#body= を呼ばれると response の @stream が再生成されてしまう。で、render_to_string が controller#response_body= で body を実行前の状態にもどすんだけど、それは ActionController::Live でオーバーライドされていて、response.stream.close を呼ぶ。

なぜかというと、ActionController::Live でも render を明示的に読んだ場合は普通に render してコネクションを閉じるという仕様だった。

とりあえず、render_to_string も ActionController::Live でオーバーライドして response.stream もリストアしてあげるようにして、 Rails に初 pull request を果たした。

Backfilled at , Published at