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

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",指示丢失文件的名称.

  • 激活运行时编译不是解决方案,因为我们采取了性能影响.解决方案是修复阻止资产预编译发生的核心问题. (34认同)
  • 为了清楚起见,config.assets.compile选项位于config/environments/production.rb中(如果您正在生产中).如果您希望能够在生产中进行实时/延迟编译,还需要在application.rb中启用延迟编译. (7认同)
  • 我使用RAILS_ENV =生产包exec rake资产预编译资产:预编译.为什么我收到此错误并需要设置此标志? (5认同)
  • 您可能想尝试一下本文所说的内容:http://devcenter.heroku.com/articles/rails31_heroku_cedar(我还没有尝试过) (3认同)
  • 谢谢你的答复.我已经做到了,但没有成功. (2认同)
  • @Tony,因为你可能会要求application.css/js/image文件以外的东西,而你没有在`application.rb`上注册它.添加/编辑`application.rb`:`config.assets.precompile + =%w(first.css second.js)`.现在这些文件也将编译.如果仅在链接`require`中使用它们,请不要添加所有文件,但仅当您使用`<link>`/`<script>时包含它们时才添加它们 (2认同)

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

  • 我无法相信找到如何做到这一点有多难. (14认同)
  • 这似乎是一个明显更好的选择:将live compile设置为true"使用更多内存,执行效果比默认值更差,不推荐使用." http://guides.rubyonrails.org/asset_pipeline.html#live-compilation (3认同)
  • 如果您要运行上述命令,请确保您已将环境设置为生产模式,或者在命令前加上RAILS_ENV = production (3认同)
  • @Underworld Bundler是一个用于管理ruby应用程序中的gem依赖项的工具,它内置于Rails 3.如果你使用的是bundle,运行`bundle exec rake ...`将确保你正在加载正确的rake和相关的依赖项你的应用.如果你不使用bundler,你只需运行`rake ...`. (2认同)

小智 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)

  • 只是想澄清后来遇到这个问题的人,上面需要更改的代码位于`config/application.rb`中 (2认同)