rails 3.1.0 ActionView::Template::Error (application.css未预编译)

我用一个简单的带有索引函数的页面控制器做了一个基本的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
107390 次浏览

默认情况下,Rails假定你已经在生产环境中预编译了你的文件,如果你想在生产环境中使用实时编译(在运行时编译你的资产),你必须设置Config.assets.compile设为true

# config/environments/production.rb
...
config.assets.compile = true
...

当您使用预编译的资产,但有任何丢失的预编译文件时,您可以使用此选项回退到链轮。

如果config.assets.compile选项设置为false,并且有丢失的预编译文件,您将得到一个“AssetNoPrecompiledError”,指示丢失的文件的名称。

如果在生产环境中将config.assets.compile设置为false,您将在生产环境中获得更好的性能。Rb和预编译你的资产。你可以用这个rake任务预编译:

bundle exec rake assets:precompile
如果您正在使用Capistrano,版本2.8.0有一个在部署时处理此问题的配方。要了解更多信息,请参阅资产管道指南的“生产中”部分: http://guides.rubyonrails.org/asset_pipeline.html < / p >

下面是快速解决方法:

如果你正在使用capistrano,那么就这样把它添加到你的部署中。

after 'deploy:update_code' do
run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
end

帽部署

一个快速修复capistrano用户是把这一行到Capfile

# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'

我今天遇到了这个错误消息,并想将解决方案发布到我的特定情况。原来,我的问题是,我的css文件之一是缺少一个结束大括号,这是导致文件不被编译。如果您有一个为生产环境设置一切(包括资产预编译)的自动化过程,那么可能很难注意到这一点。

我在开发环境中也犯了完全相同的错误。最后,我所需要做的就是修改它:

config.assets.manifest = Rails.root.join("public/assets")

到我的配置/环境/开发。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

好吧,我也遇到过同样的问题。我不想使用"config.assets.compile = true" -我必须将我所有的.css文件添加到config/environments/production.rb的列表中:

config.assets.precompile += %w( carts.css )

然后我必须创建(然后删除)tmp/restart.txt

我一直使用stylesheet_link_tag helper,所以我找到了我需要添加的所有额外的css文件:

find . \( -type f -o -type l \) -exec grep stylesheet_link_tag {} /dev/null \;

在heroku服务器(只读文件系统), 如果你想要css的运行时编译(不建议,但你可以这样做),确保你已经做了如下设置-

# inside config/application.rb
config.assets.enabled = true
config.assets.prefix = Rails.root.join('tmp/assets').to_s


# If you are using sass then keep gem outside of asset group
gem 'sass-rails',   '3.1.4'


# inside config/environments/production.rb
config.assets.compile = true

在一切都失败之后……

我的解决方案是改变布局文件从

= stylesheet_link_tag "reset-min", 'application'

= stylesheet_link_tag 'application'

它成功了!(您可以将重置文件放在清单中。)

对于所有正在阅读本文但对application.css没有问题的人,而是对他们的自定义CSS类,如admin.cssbase.css等。

解决方案是使用如上所述

bundle exec rake assets:precompile

样式表中的引用只引用application.css

<%= stylesheet_link_tag    "application", :media => "all" %>

因为assets pipeline会预编译application.css中的所有样式表。这在开发中也会发生,所以在使用资产管道时使用任何其他引用都是错误的。

我也有这个问题,试图在不预编译的情况下在生产环境中运行,仍然会抛出未预编译的错误。我不得不改变哪一行是注释应用程序。

  # 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)

如果你认为你做的一切都很好,但仍然不走运,只要确保你/capistrano在最后运行touch tmp/restart.txt或同等的命令。我在不幸的名单上,但现在:)

在Heroku上解决这个问题的另一种方法是:确保你的Rakefile被提交和推送。

你可能在你使用的css中有一个syntax error

执行此命令

$ bundle exec rake assets:precompile RAILS_ENV=development --trace

它会给出一个异常,修正后就完成了。

谢谢