rails 3.1.0 ActionView::模板::错误(应用程序。css不是预编译)

时间:2021-02-09 20:41:50

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

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

14 个解决方案

#1


312  

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. asset. 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.compile option is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.

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

#2


200  

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:

如果在生产中将config. asset. compile设为false,则会在生产中获得更好的性能。rb并预编译您的资产。您可以使用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提供了在部署时处理这个问题的方法。有关更多信息,请参阅资产管道指南的“In Production”部分:http://guides.rubyonrails.org/asset_流水线

#3


31  

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. asset. compile = true”—我必须在config/environment /product .rb中将所有的.css文件添加到列表中。

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:

我一直使用样式表_link_tag助手,所以我找到了所有需要添加的css文件:

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

#4


30  

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'

#5


11  

For all those who are reading this but do not have problem with application.css and instead with their custom CSS classes e.g. admin.css, base.css etc.

对于所有正在阅读本文但对应用没有问题的人。css和他们的自定义css类,例如admin。css,基地。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.

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

#6


8  

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:

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

#7


5  

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:

我也有这个问题,在没有预编译的情况下运行在生产中,它仍然会抛出未预编译的错误。我必须更改注释的行。

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

#8


4  

Here's the quick fix:

快速修复方案:

If you're using capistrano do this add this to your deploy.rb:

如果您正在使用capistrano,请将此添加到您的部署中。

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

cap deploy

帽部署

#9


2  

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文件丢失了一个关闭括号,导致文件无法编译。如果您有一个为生产环境设置一切(包括资产预编译)的自动化流程,那么您可能很难注意到这一点。

#10


1  

After all else failed...

毕竟其他失败……

My solution was to change the layout file from

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

= stylesheet_link_tag "reset-min", 'application'

to

= stylesheet_link_tag 'application'

And it worked! (You can put the reset file inside the manifest.)

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

#11


1  

Just another way to fix this up on Heroku: Make sure your Rakefile is committed and pushed.

另一种方法来修复Heroku:确保您的Rakefile被提交和推动。

#12


0  

On heroku server (readonly filesystem), If you want runtime compilation of css (its not recommended but you can do it), make sure you have done settings like below -

在heroku服务器(readonly文件系统)上,如果您想要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

#13


0  

if you think you followed everything good but still unlucky, just make sure you/capistrano run touch tmp/restart.txt or equivalent at the end. I was in the unlucky list but now :)

如果你觉得自己什么都做得很好,但还是不走运,那就确保你/capistrano运行touch tmp/restart。txt或相等的结尾。我在不幸的名单上,但现在:

#14


0  

You probably have a syntax error in the css you are using.

您可能在使用的css中有语法错误。

Run this command

运行这个命令

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

It will give the exception, fixed that and you are all done.

它会给出一个例外,修正了这个问题,你们都做完了。

Thanks

谢谢

#1


312  

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. asset. 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.compile option is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.

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

#2


200  

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:

如果在生产中将config. asset. compile设为false,则会在生产中获得更好的性能。rb并预编译您的资产。您可以使用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提供了在部署时处理这个问题的方法。有关更多信息,请参阅资产管道指南的“In Production”部分:http://guides.rubyonrails.org/asset_流水线

#3


31  

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. asset. compile = true”—我必须在config/environment /product .rb中将所有的.css文件添加到列表中。

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:

我一直使用样式表_link_tag助手,所以我找到了所有需要添加的css文件:

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

#4


30  

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'

#5


11  

For all those who are reading this but do not have problem with application.css and instead with their custom CSS classes e.g. admin.css, base.css etc.

对于所有正在阅读本文但对应用没有问题的人。css和他们的自定义css类,例如admin。css,基地。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.

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

#6


8  

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:

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

#7


5  

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:

我也有这个问题,在没有预编译的情况下运行在生产中,它仍然会抛出未预编译的错误。我必须更改注释的行。

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

#8


4  

Here's the quick fix:

快速修复方案:

If you're using capistrano do this add this to your deploy.rb:

如果您正在使用capistrano,请将此添加到您的部署中。

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

cap deploy

帽部署

#9


2  

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文件丢失了一个关闭括号,导致文件无法编译。如果您有一个为生产环境设置一切(包括资产预编译)的自动化流程,那么您可能很难注意到这一点。

#10


1  

After all else failed...

毕竟其他失败……

My solution was to change the layout file from

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

= stylesheet_link_tag "reset-min", 'application'

to

= stylesheet_link_tag 'application'

And it worked! (You can put the reset file inside the manifest.)

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

#11


1  

Just another way to fix this up on Heroku: Make sure your Rakefile is committed and pushed.

另一种方法来修复Heroku:确保您的Rakefile被提交和推动。

#12


0  

On heroku server (readonly filesystem), If you want runtime compilation of css (its not recommended but you can do it), make sure you have done settings like below -

在heroku服务器(readonly文件系统)上,如果您想要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

#13


0  

if you think you followed everything good but still unlucky, just make sure you/capistrano run touch tmp/restart.txt or equivalent at the end. I was in the unlucky list but now :)

如果你觉得自己什么都做得很好,但还是不走运,那就确保你/capistrano运行touch tmp/restart。txt或相等的结尾。我在不幸的名单上,但现在:

#14


0  

You probably have a syntax error in the css you are using.

您可能在使用的css中有语法错误。

Run this command

运行这个命令

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

It will give the exception, fixed that and you are all done.

它会给出一个例外,修正了这个问题,你们都做完了。

Thanks

谢谢