Rails 资产位于子目录中,无法在生产环境中加载

Rails assets are in a sub directory and cannot load in production

目前我的事件 CSS 在 app/assets/stylesheets/pages/events.scss 中,在开发模式下它看起来很好并且资产已加载。但是在生产环境中,这些页面似乎根本没有加载。我不确定如何解决这个问题。我看过 Rails 4: assets not loading in production 但我不熟悉 config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb) 因为它似乎路径本身在生产中不起作用。

app/assets/stylesheets/application.scss:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *


 *#= require bootstrap-wysihtml5
 *#= require rails_bootstrap_forms
 *#= require jquery.jcrop
 *= require custom
 *= require_self 
*/

@import "bootstrap-sprockets";
@import "bootstrap";
@import "scaffolds";

app/views/events/index.html.erb:

<% content_for :head do %>
  <%= stylesheet_link_tag "pages/events", :media => "all" %>
<% end %>

app/assets/stylesheets/pages/events.scss

@import "globals/helpers";
@import "vendors/animate";

.events-bar {
/*  a[class*="btn"] {
  height: 36px;
  display: inline-block;
}*/
#toggleBar {
  margin-top: 14px;
}
.btn-dashboard { 
   background-color: #fdf9ff;
   color: $eventsColour !important;  
   &:hover {
     background-color: $eventsColour-dark !important;
   }
 }
}

很难调试,因为它在开发中运行良好,但在生产中运行不佳。看起来子目录 (app/assets/stylesheet/pages/*.scss) 中的所有文件都无法在生产环境中运行。任何见解都会有所帮助,谢谢。

在您的 application.scss 中,您应该添加:

*= require pages/events

运行 bundle exec rake assets:precompile

让我知道它是否修复了错误,如果没有,请告诉我什么错误消息?

您只需要将事件添加到您的预编译资产列表中,因为它不是用 application.scss 编译的,您想要的格式是

config.assets.precompile += %w( pages/events.scss )

然后

bundle exec rake assets:precompile

我在 Rails version - 5.2.3
上遇到了同样的问题 资产文件位于-

app/assets/images/sub-folder

add all images were in sub-folder but was not loading on production whereas working fine on local.

图像位置在 css-

url(./sub-folder/b2.jpg)

这是我所做的-

/config/environments/production.rb 设置中-

config.assets.compile = true

最初是 false,我只是将代码推送到服务器上,发现一切正常!

希望对你也有用!!!