2011-11-08 23 views
6

Tôi có một ứng dụng Sinatra nhỏ bao gồm mô-đun này:Sinatra ứng dụng với sprockets không làm việc trên 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 

và với lộ trình sau đây định nghĩa:

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

Tất cả mọi thứ hoạt động giống lớn, tài sản được nạp và được hiển thị đúng trên máy cục bộ của tôi bằng cách sử dụng pow. Nhưng trên Heroku không có nội dung đơn lẻ nào được tải, máy chủ chỉ trả về 404 cho mọi tệp nội dung.

+0

Bạn đã thử: https://github.com/jeffrydegrande/sprockets_on_heroku, có [câu hỏi tương tự cho đường ray] tương tự (http://stackoverflow.com/questions/2530584/how- to-use-sprockets-rails-plugin-on-heroku) – topek

+0

bạn có chắc chắn rằng điều này có liên quan đến sprockets không? 404 cho biết tài nguyên bị thiếu. bạn đã đặt tuyến đường của bạn một nơi nào đó nó có thể không được công nhận bởi heroku? tệp config.ru của bạn được định cấu hình đúng cách? – phoet

+0

nơi bạn đã nhận được '"/asset/# {Environment.instance.find_asset (nguồn) .digest_path} "' ??, phiên bản nào của Sprokets ?? – Synxmax

Trả lời

4

Đơn giản hóa mô-đun và giờ nó hoạt động! Weird ...

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 question ... làm cách nào để sử dụng điều này trong sinatra..where để đặt mã này/cách tôi thêm javascripts/css/images vào asset.js và assets.css .. – coool

Các vấn đề liên quan