我试图使用Rails.application.mIDdleware,但这不起作用,因为它只包装中间件类而不是它们的实例.
现在我正在使用Ruby的instance_variable_get和is_a?从Rails.application.app开始走中间件链,但这感觉不对,特别是因为中间件没有指定方式存储上下文.例如,Rack :: Cache :: Context将下一个实例存储在名为@backend的变量中,而大多数其他实例使用@app.
有没有更好的方法来查找中间件实例?
解决方法 您可以将中间件添加到机架环境中,如下例所示:require 'rack'class MyMIDdleware attr_accessor :add_response def initialize app @app = app end def call env env['my_mIDdleware'] = self # <-- Add self to the rack environment response = @app.call(env) response.last << @add_response response endendclass MyApp def call env env['my_mIDdleware'].add_response = 'World!' # <-- Access the mIDdleware instance in the app [200,{'Content-Type'=>'text/plain'},['Hello']] endenduse MyMIDdlewarerun MyApp.new总结
以上是内存溢出为你收集整理的ruby-on-rails – 如何访问Rack中间件的特定实例?全部内容,希望文章能够帮你解决ruby-on-rails – 如何访问Rack中间件的特定实例?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)