Rails 4:资产未在生产中加载 [英] Rails 4: assets not loading in production

查看:44
本文介绍了Rails 4:资产未在生产中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的应用程序投入生产,并且图像和CSS资产路径无效.

I'm trying to put my app into production and image and css asset paths aren't working.

这是我目前正在做的事情:

Here's what I'm currently doing:

  • 图像资产位于/app/assets/images/image.jpg
  • 样式表位于/app/assets/stylesheets/style.css
  • 在我的布局中,我这样引用css文件:<%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • 在重新启动独角兽之前,我运行RAILS_ENV=production bundle exec rake assets:precompile并且成功,并且在public/assets目录中看到了指纹文件.
  • Image assets live in /app/assets/images/image.jpg
  • Stylesheets live in /app/assets/stylesheets/style.css
  • In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompile and it succeeds and I see the fingerprinted files in the public/assets directory.

浏览到我的站点时,出现mysite.com/stylesheets/styles.css的404 not found错误.

When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.

我在做什么错了?

更新: 在我的布局中,它看起来像这样:

Update: In my layout, it looks like this:

<%= stylesheet_link_tag    "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag    "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

生成源是这样的:

<link data-turbolinks-track="true" href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>

看起来像Rails不能正确地寻找已编译的CSS文件.但这很令人困惑,为什么 对于javascript仍然可以正常工作(请注意/assets/****.js路径).

Looks like Rails is not properly looking for the compiled css files. But it's very confusing why it's working correctly for javascripts (notice the /assets/****.js path).

推荐答案

/config/environments/production.rb中,我必须添加以下内容:

In /config/environments/production.rb I had to add this:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

.js已经进行了预编译,但是我还是添加了它. .css和.css.erb显然不会自动发生. ^[^_]将部分代码排除在编译范围之外-这是一个正则表达式.

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

令人沮丧的是,这些文档清楚地指出资产管道默认情况下处于启用状态,但并未阐明仅适用于javascript的事实.

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

这篇关于Rails 4:资产未在生产中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆