2011-11-08 66 views
6

鏈輪我有一點西納特拉的應用程序,包括該模塊:西納特拉應用與不工作在Heroku

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。

+0

你試過:https://github.com/jeffrydegrande/sprockets_on_heroku,也有類似的[鋼軌問題(http://stackoverflow.com/questions/2530584/how- to-use-sprockets-rails-plugin-on-heroku) – topek

+0

你確定這與鏈輪有關嗎? 404表示缺少資源。你把你的路線放在什麼地方,它可能不被heroku識別?你的config.ru文件是否配置正確? – phoet

+0

你在哪裏得到這個''/ assets /#{Environment.instance.find_asset(source).digest_path}「',哪個版本的Sprokets? – Synxmax

回答

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 
+0

newby的問題...我如何在sinatra中使用它..在哪裏放這段代碼/如何將javascripts/css/images添加到assets.js和assets.css .. – coool