带有链轮的Sinatra应用程序无法在Heroku上运行

时间:2011-11-08 18:49:42

标签: ruby heroku sinatra sprockets ruby-1.9

我有一个小的Sinatra应用程序,包括这个模块:

module Sprockets
  module Helpers
    def asset_path(source)
      "/assets/#{Environment.instance.find_asset(source).digest_path}"
    end

    def sprockets
      Environment.instance.call(env)
    end
  end

  class << self
    def precompile
      dir = 'public/assets'

      FileUtils.rm_rf(dir, secure: true)
      ::Sprockets::StaticCompiler.new(Environment.instance, 'public/assets', [/\.(png|jpg)$/, /^(application|ie)\.(css|js)$/]).compile
    end
  end

  class Environment < ::Sprockets::Environment
    include Singleton

    def initialize
      super
      %w[app lib vendor].each do |dir|
        %w[images javascripts stylesheets].each do |type|
          path = File.join(root, dir, 'assets', type)
          append_path(path) if File.exist?(path)
        end
      end

      js_compressor = Uglifier.new
      css_compressor = YUI::CssCompressor.new

      context_class.instance_eval do
        include Helpers
      end
    end
  end
end

并定义了以下路线:

get('/assets/*') do
  sprockets # Defined in the module above
end

一切正常,资产在我的本地计算机上使用pow正确加载和显示。但是在Heroku上没有加载任何单个资产,服务器只为每个资产文件返回404。

1 个答案:

答案 0 :(得分:4)

简化模块,现在可以正常使用!奇怪的...

class Assets < Sprockets::Environment
  class << self
    def instance(root = nil)
      @instance ||= new(root)
    end
  end

  def initialize(root)
    super

    %w[app lib vendor].each do |dir|
      %w[images javascripts stylesheets].each do |type|
        path = File.join(root, dir, 'assets', type)
        self.append_path(path) if File.exist?(path)
      end
    end

    self.css_compressor = YUI::CssCompressor.new
    self.js_compressor = Uglifier.new

    context_class.instance_eval do
      include Helpers
    end
  end

  def precompile
    dir = 'public/assets'

    FileUtils.rm_rf(dir, secure: true)
    Sprockets::StaticCompiler.new(self, 'public/assets', ['*']).compile
  end

  module Helpers
    def asset_path(source)
      "/assets/#{Assets.instance.find_asset(source).digest_path}"
    end
  end
end