Rails [assets 关联] 这个错误是怎么回事?

zhangjingqiang · 2012年01月20日 · 最后由 zhangjingqiang 回复于 2012年02月04日 · 3586 次阅读

■错误信息 Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Index#index

Showing ../app/views/index/index.html.erb where line #2 raised:

index.css isn't precompiled

Extracted source (around line #2):

1: <% content_for(:ex_styles) do %> 2: <%= stylesheet_link_tag "index" %> 3: <% end %> 4: <% content_for :ex_scripts do %> 5: <%= javascript_include_tag "index" %> 6: <% end %>

■RoR 版本 Ruby 1.9.3p0 Rails 3.1.3

■尝试内容 在 Google 上搜索,说“config/enviroments/production.rb”中的 config.assets.compress = true 是必要的,我的默认环境是这样的,然而相同目录的“development.rb”中的代码却是: config.assets.compress = false 把它改成 true,执行: bundle exec rake assets:precompile 得到的信息是: .rvm/gems/ruby-1.9.3-p0/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets .rvm/gems/ruby-1.9.3-p0/bin/rake assets:precompile:nondigest RAILS_ENV=production RAILS_GROUPS=assets 似乎只对 production 生效? 刷新页面依然是那个错误信息。

开发环境不需要 precompile,只有生产环境才需要 详见 http://guides.rubyonrails.org/asset_pipeline.html

谢谢回答。 看来是我部署项目的问题。 我用 Apache+Passenger,在 Apache 的配置文件中加上了自动加载 Passenger 的设置,自动找生产环境了?

如果在 prod 上用 pre compile 的话,不是所有的 css 和 js 都会被预编译,拿 css 来说,他是根据 application.css 中的引用来判断,如果你的 css 没有被包含在 application.css 或者其中引用的 css 中,那么这个资源就会不被预编译,prod 上就会出错。

你需要在 production.rb 中指定需要预编译的资源,如下:

config.assets.precompile += %w( login.css controllers/*.js controllers/*.css lib/*.js lib/*.css common/*.js )

是 Web 服务器的问题。 我参照这里安装了 libapache2-mod-passenger: http://www.modrails.com/install.html 那个问题没有了。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号