Ruby-on-rails rails 3.1.0 ActionView::Template::Error(application.css 未预编译)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7275636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails 3.1.0 ActionView::Template::Error (application.css isn't precompiled)
提问by Chris Muench
I made a basic rails app with a simple pages controller with an index function and when I load the page I get:
我制作了一个带有索引功能的简单页面控制器的基本 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'
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
回答by Chris Muench
By default Rails assumes that you have your files precompiled in the production environment, if you want use live compiling (compile your assets during runtime) in production you must set the config.assets.compile to true.
默认情况下,Rails 假定您在生产环境中预编译了您的文件,如果您想在生产中使用实时编译(在运行时编译您的资产),您必须将config.assets.compile设置为 true。
# config/environments/production.rb
...
config.assets.compile = true
...
You can use this option to fallback to Sprockets when you are using precompiled assets but there are any missing precompiled files.
当您使用预编译资产但缺少任何预编译文件时,您可以使用此选项回退到链轮。
If config.assets.compileoption is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.
如果config.assets.compileoption 设置为 false 并且缺少预编译文件,您将收到一个“AssetNoPrecompiledError”,指示丢失文件的名称。
回答by richardsun
You will get better performance in production if you set config.assets.compile to false in production.rb and precompile your assets. You can precompile with this rake task:
如果您在 production.rb 中将 config.assets.compile 设置为 false 并预编译您的资产,您将在生产中获得更好的性能。您可以使用此 rake 任务进行预编译:
bundle exec rake assets:precompile
If you are using Capistrano, version 2.8.0 has a recipe to handle this at deploy time. For more info, see the "In Production" section of the Asset Pipeline Guide: http://guides.rubyonrails.org/asset_pipeline.html
如果您使用 Capistrano,2.8.0 版有一个在部署时处理此问题的方法。有关更多信息,请参阅资产管道指南的“生产中”部分:http: //guides.rubyonrails.org/asset_pipeline.html
回答by Don Law
OK - I had the same problem. I didn't want to use "config.assets.compile = true" - I had to add all of my .css files to the list in config/environments/production.rb:
好的 - 我遇到了同样的问题。我不想使用“config.assets.compile = true”——我必须将我所有的 .css 文件添加到 config/environments/production.rb 的列表中:
config.assets.precompile += %w( carts.css )
Then I had to create (and later delete) tmp/restart.txt
然后我不得不创建(然后删除)tmp/restart.txt
I consistently used the stylesheet_link_tag helper, so I found all the extra css files I needed to add with:
我一直使用 stylesheet_link_tag 助手,所以我找到了所有需要添加的额外 css 文件:
find . \( -type f -o -type l \) -exec grep stylesheet_link_tag {} /dev/null \;
回答by user673207
A quick fix for capistrano user is to put this line to Capfile
capistrano 用户的快速修复是将此行放入 Capfile
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
回答by Haris Krajina
For all those who are reading this but do not have problem with application.cssand instead with their custom CSS classes e.g. admin.css, base.cssetc.
对于所有正在阅读本文但对application.css他们的自定义 CSS 类没有问题的人,例如admin.css,base.css等等。
Solution is to use as mentioned
解决方案是如上所述使用
bundle exec rake assets:precompile
And in stylesheets references just reference application.css
而在样式表中引用只是参考 application.css
<%= stylesheet_link_tag "application", :media => "all" %>
Since assets pipeline will precompile all of your stylesheets in application.css. This also happens in development so using any other references is wrong when using assets pipeline.
由于资产管道将预编译 application.css 中的所有样式表。这也发生在开发中,因此在使用资产管道时使用任何其他引用都是错误的。
回答by darchitect
I was having the exact same error in my development environment. In the end all I needed to do in order to fix it was to add:
我在我的开发环境中遇到了完全相同的错误。最后,为了修复它,我需要做的就是添加:
config.assets.manifest = Rails.root.join("public/assets")
to my config/environments/development.rb file and it fixed it. My final config in development related to assets looks like:
到我的 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
回答by mathdancer
I also had this issue, where trying to run in production without precompiling it would still throw not-precompiled errors. I had to change which line was commented application.rb:
我也遇到了这个问题,在没有预编译的情况下尝试在生产中运行它仍然会抛出未预编译的错误。我不得不更改注释为 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)
回答by fivetwentysix
Here's the quick fix:
这是快速修复:
If you're using capistrano do this add this to your deploy.rb:
如果您使用的是 capistrano,请将此添加到您的 deploy.rb 中:
after 'deploy:update_code' do
run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
end
cap deploy
上限部署
回答by SnapShot
I ran into this error message today and wanted to post the resolution to my particular my case. It turns out that my problem was that one of my css files was missing a closing brace and this was causing the file to not be compiled. It may be harder to notice this if you have an automated process that sets everything up (including the asset precompile) for your production environment.
我今天遇到了这个错误消息,想将解决方案发布到我的特定案例中。事实证明,我的问题是我的一个 css 文件缺少右括号,这导致文件无法编译。如果您有一个为生产环境设置所有内容(包括资产预编译)的自动化流程,则可能更难注意到这一点。
回答by phillbaker
Just another way to fix this up on Heroku: Make sure your Rakefile is committed and pushed.
在 Heroku 上解决这个问题的另一种方法:确保您的 Rakefile 已提交并推送。

