Hello World 机架中间件与 Rails 3:如何处理所有请求的正文

发布于 2024-10-14 09:32:32 字数 686 浏览 4 评论 0原文

我想尝试一个简单的机架中间件“hello world”,但我似乎陷入困境。 看起来主要语法发生了变化,因为一些示例使用了以下代码:

require 'rack/utils'

class FooBar

  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
         body.body << "\nHi from #{self.class}"
         [status, headers, body]
  end
end

产生错误:

undefined method `<<' for #<ActionDispatch::Response:0x103f07c48>

即使我查看其他代码,我似乎也无法让它们与 Rails 3.0.3 一起运行。

这是我的具体问题:

  • 如何获得一个简单的机架中间件来运行并修改 Rails 应用程序的任何输出的正文?
  • 我应该将 Rails.application.config.middleware.use 声明放在哪里? (我为此在配置/初始化程序中创建了一个自己的初始化程序)

提前非常感谢!

i want to try out a simple rack middleware "hello world", but i seem to get stuck.
it looks like the main sytax changed, since some examples use this code:

require 'rack/utils'

class FooBar

  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
         body.body << "\nHi from #{self.class}"
         [status, headers, body]
  end
end

produces an error:

undefined method `<<' for #<ActionDispatch::Response:0x103f07c48>

even when i look at other codes out there, i cannot seem to get them running with rails 3.0.3.

here are my concrete questions:

  • how can i get a simple rack middleware to run and modify the body of any output from a rails app?
  • where should i put the Rails.application.config.middleware.use declaration? (i created an own initializer in config/initializers for that)

thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-10-21 09:32:32

这应该做你想要的事情:

# in config/application.rb
config.middleware.use 'FooBar'

# in config/initializers/foo_bar.rb
class FooBar
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    [status, headers, response.body << "\nHi from #{self.class}"]
  end
end

请注意,对于几乎所有其他请求(至少在 Rails 3.0.3 上),这将由于另一个中间件(Rack::Head)而失败,因为它在内容时发送一个空请求是不变的。在这个例子中,我们依赖于能够调用response.body,但事实上,数组的最后一个成员可以是响应.each的任何内容。

Ryan Bates 在这里很好地介绍了 Rack:

http://asciicasts.com/episodes/151-rack-中间件

http://railscasts.com/episodes/151-rack-middleware

官方 Rails 指南也相当不错:

http://guides.rubyonrails.org/rails_on_rack.html< /a>

当然还有官方 Rack 规范:

http://rack.rubyforge.org/doc/规格.html

This should do what you want it to:

# in config/application.rb
config.middleware.use 'FooBar'

# in config/initializers/foo_bar.rb
class FooBar
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    [status, headers, response.body << "\nHi from #{self.class}"]
  end
end

Be advised, that on just about every other request (at least on Rails 3.0.3), this will fail due to another middleware (Rack::Head) because it sends an empty request when content is unchanged. We are in this example depending on being able to call response.body, but in fact, the last member of the array can be anything that responds to .each.

Ryan Bates goes over Rack pretty well here:

http://asciicasts.com/episodes/151-rack-middleware

http://railscasts.com/episodes/151-rack-middleware

And the official Rails guide is pretty good too:

http://guides.rubyonrails.org/rails_on_rack.html

And of course the official Rack spec:

http://rack.rubyforge.org/doc/SPEC.html

今天小雨转甜 2024-10-21 09:32:32

Rails 3.2.12+

上一个答案不适用于 Rails 3.2.12+

这个做:

# in config/application.rb
config.middleware.use 'FooBar'

# in config/initializers/foo_bar.rb
class FooBar
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    response.body += "\nHi from #{self.class}"
    # response.body << "..." WILL NOT WORK
    [status, headers, response]
  end
end

Rails 3.2.12+:

previous answer does not work for Rails 3.2.12+

This one does:

# in config/application.rb
config.middleware.use 'FooBar'

# in config/initializers/foo_bar.rb
class FooBar
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    response.body += "\nHi from #{self.class}"
    # response.body << "..." WILL NOT WORK
    [status, headers, response]
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文