【问题标题】:Rails assets are in a sub directory and cannot load in productionRails 资产位于子目录中,无法在生产环境中加载
【发布时间】:2016-11-06 21:24:26
【问题描述】:

目前我的事件 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)中的所有文件都不能在生产环境中工作。任何见解都会有所帮助,谢谢。

【问题讨论】:

  • 你能分享你的 Gemfile 吗?

标签: css ruby-on-rails production-environment


【解决方案1】:

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

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

然后

bundle exec rake assets:precompile

【讨论】:

  • 这对我有用,但我必须对 pages 子目录中的所有 scss 文件使用 config.assets.precompile += %w( pages/file.scss )。有没有更好的方法(我可以使用通配符,如:*.scss)吗?子目录中的文件的目的是保持代码更干净,并且只加载某些需要的页面。抱歉,我是这个结构的新手……我通常只有 assets/stylesheets/ 下的 scss 文件,仅此而已。
  • 您可以让单个文件在顶部有一个像 application.scss 那样的 require 结构列表,然后只指定一个,它将包括其余的。如果您想要pages/. 中的所有内容,那么您可以在其中使用//= require_tree . 来创建一个文件
【解决方案2】:

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

*= require pages/events

运行bundle exec rake assets:precompile

让我知道它是否修复了错误,或者它没有什么错误消息?

【讨论】:

    【解决方案3】:

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

    app/assets/images/sub-folder
    

    添加所有图像都在 sub-folder 但在生产中未加载,而在本地工作正常。

    css中的图片位置-

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

    这就是我所做的-

    /config/environments/production.rb设置-

    config.assets.compile = true
    

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

    希望对你也有用!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      相关资源
      最近更新 更多