我正在使用Sass :: Engine动态编译css文件,我需要传递环境并将路径加载到sprockets。这在开发中效果很好:
def compile_scss
view_context = ActionView::Base.new
environment = Rails.application.assets
# body and filename are instance variables created elsewhere
@compiled_file = Sass::Engine.new(body,
filename: filename,
syntax: :scss,
cache: false,
load_paths: environment.paths,
sprockets: {
context: view_context,
environment: environment
}).render
end
但生产失败,因为Rails.application.assets
为nil
。什么是生产中的等价物?
答案 0 :(得分:1)
Rails.application.assets在生产中总是nil
,因为资产是从公共文件夹中预编译并提供的(感谢@ bkunzi01)。此外,Sprockets只需要将资产路径传递给它以便运行,而不是环境。这些路径可从Rails.application.config.assets.paths
获得。我能够让我的样式表编译器使用以下方法。
def compile_scss
view_context = ActionView::Base.new
environment = Rails.application.assets || Rails.application.config.assets
# body and filename are instance variables created elsewhere
@compiled_file = Sass::Engine.new(body,
filename: filename,
syntax: :scss,
cache: false,
load_paths: environment.paths,
sprockets: {
context: view_context,
}).render
end