16

我有一个 Rails 4 示例项目(博客),并且我创建了一个名为“request_timer”的简单中间件config/initializers/request_timer.rb

  #config/initializers/request_timer.rb
  class RequestTimer                                                                                                                                         

    def initialize(app)                                                                                                                                      
     @app = app                                                                                                                                              
    end                                                                                                                                                      

    def call(env)                                                                                                                                            
      start_time = Time.now                                                                                                                                  
      status, headers, response = @app.call(env)                                                                                                             
      stop_time = Time.now                                                                                                                                   
      [status, headers, response.body]                                                                                                                       
    end                                                                                                                                                      

  end 

config/application.rb以两种方式添加了我的中间件

1 ) 添加为常数

#config/application.rb
module Blog                                                                                                                                                  
  class Application < Rails::Application                                                                                                                     
    config.middleware.use RequestTimer                                                                                                                       
  end                                                                                                                                                        
end

这样,当我尝试运行我的 rails 应用程序时,我得到了错误

/Users/sameera/workspace/ruby-rack/blog/config/application.rb:9:in `require': cannot load such file -- request_timer (LoadError)
    from /Users/sameera/workspace/ruby-rack/blog/config/application.rb:9:in `<top (required)>'
    from /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:74:in `require'
    from /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:74:in `block in <top (required)>'
    from /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap'
    from /Users/sameera/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

2)然后我将中间件添加为字符串

#config/application.rb
module Blog                                                                                                                                                  
  class Application < Rails::Application                                                                                                                     
    config.middleware.use "RequestTimer"                                                                                                                       
  end                                                                                                                                                        
end

这样,它允许我运行 rails 服务器,但是当我访问时localhost:3000,它会出错说

NoMethodError (undefined method `each' for #<String:0x007fdf649b0028>):
  rack (1.5.2) lib/rack/etag.rb:58:in `digest_body'
  rack (1.5.2) lib/rack/etag.rb:26:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__2755475928771109453__call__callbacks'
  activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.2) lib/rails/engine.rb:511:in `call'
  railties (4.0.2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/sameera/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/sameera/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/sameera/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

原因是什么?我正在使用Rails4.0.2和.rack-1.5.2ruby 2.0.0p247

4

4 回答 4

31
  1. 移动config/initializers/request_timer.rblib/request_timer.rb文件夹。
  2. require_relative '../lib/request_timer'行添加到application.rb文件。
  3. 更改config.middleware.use "RequestTimer"config.middleware.use RequestTimer。不要使用引号。

我记得我们不能使用位于 initializers 文件夹中的类而不需要它们在application.rb.

问候。

于 2014-06-09T14:28:04.207 回答
5

解决这个问题的更简单的方法是将您request_timer.rb放入 app/middleware/request_timer.rb然后将中间件作为字符串添加到您的config/application.rb

于 2016-02-23T04:26:10.503 回答
3

您应该将中间件类放在 app/middleware 文件夹中。添加的中间件应注入到默认情况下由 rails 添加的中间件列表中。

注意:您可以从代码中的任何位置插入中间件,但最好在您为应用程序添加的任何初始化程序中插入中间件。

例如,在 rails 内置中间件参数解析器之后插入中间件:

Rails.application.middleware.insert_after ActionDispatch::ParamsParser, "RequestTimer"

此外,您还可以将所需的参数传递给中间件,例如:

options = { :foo => 'bar' }
Rails.application.middleware.insert_after ActionDispatch::ParamsParser, "SecuredClient", options

然后您可以在中间件中访问这些参数,如下所示:

class RequestTimer                                                                                                                                         
  def initialize(app, params)                                                                                                                                      
    @app = app
    @params = params                                                                                          
  end
end
于 2016-06-23T17:07:31.843 回答
0

NoMethodError (undefined method 'each' for #<String:0x007fdf649b0028>),你应该确保reponse.body响应'each'方法,但它的类是String。你可以像这样修复代码:[status, headers, [response.body]]

Ruby 1.9 不再在 String 类中包含 each。

架子

要使用 Rack,请提供一个“应用程序”:一个响应调用方法的对象,将环境哈希作为参数,并返回一个包含三个元素的数组:
* HTTP 响应代码
* 标头哈希
* 响应正文,必须回应each

于 2015-04-21T05:31:14.350 回答