Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为公用文件夹中的文件设置HTTP头_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 为公用文件夹中的文件设置HTTP头

Ruby on rails 为公用文件夹中的文件设置HTTP头,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我想为从Rails中的public文件夹请求的视频文件设置Accept Ranges none。视频文件不在我的资产管道中,因此视频仅存在于/public/videos/example.mp4。如何在开发模式下设置这些HTTP头?我试图在development.rb中编辑config.public\u文件\u server.headers散列,但我认为这不是正确的配置 config.public_file_server.headers = { 'Cache-Control' => "p

我想为从Rails中的
public
文件夹请求的视频文件设置
Accept Ranges none
。视频文件不在我的资产管道中,因此视频仅存在于
/public/videos/example.mp4
。如何在开发模式下设置这些HTTP头?我试图在
development.rb
中编辑
config.public\u文件\u server.headers
散列,但我认为这不是正确的配置

config.public_file_server.headers = {
  'Cache-Control' => "public, max-age=15768000",
  "Accept-Ranges" => "none"
}

据我所知,在Rails 4中,除了
缓存控制
之外,不能对文件设置其他响应头。这是一个限制

但是,您可以在
development.rb中设置所需的任何标题,这是进行开发的正确位置:
config.public\u file\u server.headers

但是,为了使更改生效,您必须在启动服务器之前使用
railsdev:cache

演示: development.rb:

if Rails.root.join('tmp/caching-dev.txt').exist?
  config.action_controller.perform_caching = true

  config.cache_store = :dalli_store
  config.public_file_server.headers = {
    'Cache-Control' => 'public, max-age=172800',
    'Accept-Ranges' => 'none'
  }
else
  config.action_controller.perform_caching = false

  config.cache_store = :null_store
end
$ curl -sI http://localhost:3000/car-images-silhouettes/back.png | grep Accept-Ranges                                                 
Accept-Ranges: none
开发缓存和服务器

$ rails dev:cache                                                                                                                
Development mode is now being cached.

$ rails s
请求:

if Rails.root.join('tmp/caching-dev.txt').exist?
  config.action_controller.perform_caching = true

  config.cache_store = :dalli_store
  config.public_file_server.headers = {
    'Cache-Control' => 'public, max-age=172800',
    'Accept-Ranges' => 'none'
  }
else
  config.action_controller.perform_caching = false

  config.cache_store = :null_store
end
$ curl -sI http://localhost:3000/car-images-silhouettes/back.png | grep Accept-Ranges                                                 
Accept-Ranges: none

嘿,我的答案解决问题了吗?