在Heroku Cedar的Rails中为资产设置自定义标题 [英] Setting Custom Headers for Assets in Rails on Heroku Cedar

查看:104
本文介绍了在Heroku Cedar的Rails中为资产设置自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Rails 4.0和资产管道的雪松应用程序。我想为资产管道中的所有资产设置自定义标题。如何做到这一点?

I have a cedar application that uses Rails 4.0 and the asset pipeline. I'd like to set custom headers for all assets from the asset pipeline. How can this be done?

推荐答案

简单的方法是使用机架插件,如下所示:

An easy way would be to use a rack plugin, something like this:

class RackAssetFilter
   def initialize app
      @app = app
   end

   def call env
      @status, @headers, @body = @app.call env
      if env['PATH_INFO'].starts_with?( "/assets/" )
         @headers['X-Header-1'] = 'value'
         # ... 
      end
      return [@status, @headers, @body]
   end
end

要在application.rb中启用它:

To enable it, in application.rb:

config.middleware.insert_before( ActionDispatch::Static, RackAssetFilter )

请记住,您需要通过require声明或加载RackAssetFilter,然后将其插入到application.rb中的中间件堆栈中。

Keep in mind you need to declare or load the RackAssetFilter via require before you insert it into the middleware stack in application.rb

这篇关于在Heroku Cedar的Rails中为资产设置自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆