Rails - 默认渲染流

时间:2015-01-05 10:16:31

标签: ruby-on-rails ruby-on-rails-4 streaming render

通常Rails streaming要求:

render stream: true

在控制器动作中。有没有办法让它成为整个控制器的默认值? RailsCasts表示可以使用stream class method

class ApplicationController < ActionController::Base
  stream # doesn't work
  ...
end

但是这不在官方文档和Rails 4.1中,我为ApplicationController:Class`得到undefined local variable or method stream'。能够在一个地方声明流式传输以保持DRY动作是很好的。

1 个答案:

答案 0 :(得分:0)

webserver Rails 3中的默认WEBrick不支持流式传输,因此要查看它的实际情况,我们需要切换到另一个,例如{{1 }}。 Unicorn gem列在应用程序的Unicorn中,但默认情况下已注释掉。要使用它,我们取消注释该行,然后运行Gemfile进行安装。

bundle

然后

 gem 'unicorn'

Unicorn支持流媒体,但需要进行配置。为此,你

需要创建一个配置文件,如下所示:

 $ bundle install

并在初始化时使用它:

 # unicorn.config.rb
  listen 3000, tcp_nopush: false

您可能还想配置其他参数,例如 unicorn_rails --config-file unicorn.config.rb 。    Please check its documentation for more information:

如果您将Unicorn与tcp_nodelay一起使用,则可能需要调整NGINX。   流媒体应该在NGINX开箱即用。

希望它能运作

更多考虑Rails streaming