由于不同的 gem 在我的系统中如何交互,我在 rails 应用程序上安装了一个引擎。我最近开始研究一个提供一些中间件功能的新 gem。
有点像这样:
BaseApp
\
Engine
\
NewMiddlewareEngine
# BaseApp/Gemfile
gem 'Engine'
# Engine/Gemfile
gem 'NewMiddlewareEngine'
# rake middleware output:
user@laptop[BaseApp]$ bundle exec rake middleware
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1>
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::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use CatchJsonParseErrors
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
run BaseApp::Application.routes
但是,我似乎无法让 NewMiddlewareEngine 出现在中间件中。我已经测试过安装这个:
BaseApp
\
NewMiddlewareEngine
# BaseApp/Gemfile
gem 'NewMiddlewareEngine'
# rake middleware output:
user@laptop[BaseApp]$ bundle exec rake middleware
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x2f9795d8>
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::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use CatchJsonParseErrors
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use NewMiddlewareEngine # Notice it mounts fine on it's own
run BaseApp::Application.routes
和:
BaseApp
\
Engine
# BaseApp/Gemfile
gem 'Engine'
# rake middleware output:
user@laptop[BaseApp]$ bundle exec rake middleware
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1>
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::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use CatchJsonParseErrors
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
run BaseApp::Application.routes
两者都可以正常工作。问题是当我尝试“通过”引擎安装 NewMiddlewareEngine 时。
有谁知道如何配置这样的东西?
这是 MyMiddlewareEngine 安装:
module MyMiddlewareEngine
class Railtie < Rails::Railtie
initializer "add_my_middleware_engine_route_middleware" do |app|
app.middleware.use 'MyMiddlewareEngine'
end
end
end