Chr*_*nch 281 ruby-on-rails asset-pipeline
我用一个带索引功能的简单页面控制器创建了一个基本的rails应用程序,当我加载页面时,我得到:
ActionView::Template::Error (application.css isn't precompiled):
2: <html>
3: <head>
4: <title>Demo</title>
5: <%= stylesheet_link_tag "application" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__43625033_88530400'
Run Code Online (Sandbox Code Playgroud)
的Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
Run Code Online (Sandbox Code Playgroud)
Chr*_*nch 313
默认情况下,Rails假定您在生产环境中预编译了文件,如果要在生产中使用实时编译(在运行时编译资源),则必须将config.assets.compile设置为true.
# config/environments/production.rb
...
config.assets.compile = true
...
Run Code Online (Sandbox Code Playgroud)
使用预编译资产但是有任何缺少的预编译文件时,可以使用此选项回退到Sprockets.
如果config.assets.compileoption设置为false并且缺少预编译文件,则会收到"AssetNoPrecompiledError",指示丢失文件的名称.
ric*_*sun 202
如果在production.rb中将config.assets.compile设置为false并预编译资产,您将在生产中获得更好的性能.您可以使用此rake任务进行预编译:
bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Capistrano,版本2.8.0有一个配方可以在部署时处理此问题.有关详细信息,请参阅"资产管道指南"的"生产中"部分:http: //guides.rubyonrails.org/asset_pipeline.html
小智 31
好的 - 我遇到了同样的问题.我不想使用"config.assets.compile = true" - 我必须将所有.css文件添加到config/environments/production.rb中的列表中:
config.assets.precompile += %w( carts.css )
Run Code Online (Sandbox Code Playgroud)
然后我不得不创建(以后删除)tmp/restart.txt
我一直使用stylesheet_link_tag帮助器,所以我找到了我需要添加的所有额外的css文件:
find . \( -type f -o -type l \) -exec grep stylesheet_link_tag {} /dev/null \;
Run Code Online (Sandbox Code Playgroud)
use*_*207 30
对capistrano用户的快速修复是将此行放入Capfile
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
Run Code Online (Sandbox Code Playgroud)
Har*_*ina 11
对于所有正在阅读本文但没有问题的人,application.css而不是他们的自定义CSS类,例如admin.css,base.css等等.
解决方案是如上所述使用
bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)
在样式表中引用仅供参考 application.css
<%= stylesheet_link_tag "application", :media => "all" %>
Run Code Online (Sandbox Code Playgroud)
由于assets管道将在application.css中预编译所有样式表.这也发生在开发中,因此在使用资产管道时使用任何其他引用是错误的.
小智 8
我在开发环境中遇到了完全相同的错误.最后,为了解决这个问题我需要做的就是添加:
config.assets.manifest = Rails.root.join("public/assets")
Run Code Online (Sandbox Code Playgroud)
到我的config/environments/development.rb文件,它修复了它.我在开发中与资产相关的最终配置如下:
config.assets.compress = false
config.assets.precompile += %w[bootstrap-alerts.js] #Lots of other space separated files
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
config.assets.debug = true
Run Code Online (Sandbox Code Playgroud)
小智 5
我也有这个问题,试图在没有预编译的情况下在生产中运行仍会抛出未预编译的错误.我不得不改变评论的哪一行application.rb:
# If you precompile assets before deploying to production, use this line
# Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
Bundler.require(:default, :assets, Rails.env)
Run Code Online (Sandbox Code Playgroud)