2012-10-05 14 views
1

我正在使用使用鏈輪的Sinatra項目。當我在資產文件夾中添加新樣式表時,出現此錯誤:Sinatra上的鏈輪asset_path錯誤

Error compiling CSS asset

Sprockets::NotImplementedError: Custom asset_path helper is not implemented

Extend your environment context with a custom method.

environment.context_class.class_eval do 
    def asset_path(path, options = {}) 
    end 
end 

我在做什麼錯?

回答

2

我跟着錯誤消息的建議:我在config.ru定義的asset_path方法:

environment.context_class.class_eval do 
    def asset_path(path, options = {}) 
    "/assets/#{path}" 
    end 
end 

我仍然不知道爲什麼,這是需要的,但它使錯誤消失。

1

解決同樣的錯誤Padrino,我在app.rb定義這種方法,改變environmentassets

assets.context_class.class_eval do 
    def asset_path(path, options = {}) 
    "/assets/#{path}" 
    end 
end 
0

已經提出,你需要定義方法asset_path爲您的環境。

此方法使用的是常用於CSS文件的helpers like image_url, ...。您可能想要使它有所不同,具體取決於options[:type]

例子:

environment.context_class.class_eval do 
    def asset_path(path, options = {}) 
    if type = options[:type] 
     "/assets/#{type.to_s.pluralize}/#{path}" 
    else 
     "/assets/#{path}" 
    end 
    end 
end 

這樣做磨使asset_url返回/assets/path/to/your/fileimage_url助手將返回/assets/images/path/to/your/file

0

難道我們不需要鏈輪的一個實例調用? 對於我正在玩的Rails應用程序,我在config.ru中使用它來擺脫錯誤。

map '/assets' do 
    environment = Sprockets::Environment.new 
    environment.context_class.class_eval do 
    def asset_path(path, options = {}) 
     "app/assets/#{path}" 
    end 
    end 

    environment.append_path 'app/assets/javascripts' 
    environment.append_path 'app/assets/stylesheets' 
    run environment 
end