There's an echo in my head

日々のメモ。

RailsでRackミドルウェアを追加する

rakeタスクでつまれてるミドルウェアを確認する。

$ rake middleware
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fed13d07bb0>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::ParamsParser
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
use Sass::Plugin::Rack
use Bullet::Rack
run MyApp::Application.routes

ActionDispatch::Flashを使いたいので追加する。

# config/application.rb
module MyApp
  class Application < Rails::Application
       :
    # [基準となるミドルウェア, 追加するミドルウェア]の順番
    config.insert_before ActionDispatch::ParamsParser, ActionDispatch::Flash
       :
  end
end

挿入する位置を決めるときになにか基準はあるのかな?順序に依存しなければ、どこでもいいんだろうか。

参考

このブログに出てくるコードスニペッツは、引用あるいは断りがない限りMITライセンスです。