繁体   English   中英

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

[英]rails 3.1.0 ActionView::Template::Error (application.css isn't precompiled)

我用一个带索引功能的简单页面控制器创建了一个基本的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

默认情况下,Rails假定您在生产环境中预编译了文件,如果要在生产中使用实时编译(在运行时编译资产),则必须将config.assets.compile设置为true

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

使用预编译资产但是有任何缺少的预编译文件时,可以使用此选项回退到Sprockets。

如果config.assets.compile选项设置为false并且缺少预编译文件,则会得到“AssetNoPrecompiledError”,指示丢失文件的名称。

如果在production.rb中将config.assets.compile设置为false并预编译资产,您将在生产中获得更好的性能。 您可以使用此rake任务进行预编译:

bundle exec rake assets:precompile

如果您使用的是Capistrano,版本2.8.0有一个配方可以在部署时处理此问题。 有关详细信息,请参阅“资产管道指南”的“生产中”部分: http//guides.rubyonrails.org/asset_pipeline.html

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

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

然后我不得不创建(以后删除)tmp / restart.txt

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

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

对capistrano用户的快速修复是将此行放入Capfile

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

对于所有那些谁是阅读这一点,但没有有问题的application.css ,而是与他们的自定义CSS类如admin.cssbase.css等。

解决方案是如上所述使用

bundle exec rake assets:precompile

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

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

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

我在开发环境中遇到了完全相同的错误。 最后,为了解决这个问题我需要做的就是添加:

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

到我的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

我也有这个问题,试图在没有预编译的情况下在生产中运行仍会抛出未预编译的错误。 我不得不改变评论的哪一行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)

这是快速修复:

如果您正在使用capistrano,请将此添加到您的deploy.rb:

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

上限部署

我今天遇到了这个错误消息,并希望将解决方案发布到我的特定情况。 事实证明,我的问题是我的一个css文件缺少一个右括号,这导致文件无法编译。 如果您有一个自动化流程可以为您的生产环境设置所有内容(包括资产预编译),则可能更难注意到这一点。

一切都失败后......

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

= stylesheet_link_tag "reset-min", 'application'

= stylesheet_link_tag 'application'

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

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

在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

如果你认为你跟着一切都很好但仍然不走运,那么请确保你/ capistrano在最后运行触摸tmp / restart.txt或等效物。 我当时处于不吉利的名单但是现在:)

您可能在使用的CSS中有syntax error

运行此命令

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

它会给出异常,修复它,你就完成了。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM