Rails 3:如何在 application.rb 中声明 Rack 中间件 [英] Rails 3: How to declare Rack middleware in application.rb
问题描述
很多例子,比如这两个:
Many examples such as these two:
http://asciicasts.com/episodes/151-rack-middleware
在一个类中定义中间件然后添加
define middleware in a class and then add
config.middleware.use "ClassNameHere"
to config/application.rb
但我不知道在 application.rb 中的哪个位置添加它.我把它放在 class Application < 里面Rails::应用程序
.我也不确定是否有放置中间件类的特定位置.我在/lib 中有我的.
to config/application.rb
but I can't figure where in application.rb to add this. I have put it inside of class Application < Rails::Application
. I am also not sure if there is a specific location where I put my middleware class. I have mine in /lib.
假设我的中间件类称为 ResponseTimer,当我运行 rake middleware
时收到以下错误:未初始化的常量ResponseTimer
Say my middleware class is called ResponseTimer, I receive the following error when I run rake middleware
:
uninitialized constant ResponseTimer
请注意,我使用的是 Rails 3.1 而不是 2.x,这就是为什么我没有将 config.middleware.use 放在 environment.rb 中
Please note that I am in Rails 3.1 and not 2.x which is why I am not putting the config.middleware.use in environment.rb
推荐答案
add config.autoload_paths += %W(#{config.root}/lib)
到 config/application.rb
见这里 https://github.com/雷达/指南/blob/master/rails-lib-files.md
add config.autoload_paths += %W(#{config.root}/lib)
to config/application.rb
See here https://github.com/radar/guides/blob/master/rails-lib-files.md
这篇关于Rails 3:如何在 application.rb 中声明 Rack 中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!