【问题标题】:How to combine multiple css, js files and serve as one file in rails 3?如何在rails 3中合并多个css、js文件并作为一个文件?
【发布时间】:2016-07-11 10:34:12
【问题描述】:

我一直在尝试改善 Rails 3 中的页面加载时间,我经历了几次blogs,在那里我了解到我可以包含 cssjs 文件application.cssapplication.js 中的名称。

出于测试目的,我从布局中删除了文件并将其包含在 application.css 中,然后在页面重新加载时 css 文件没有重新加载。 p>

我当前的 application.css 文件:

/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.

    *
    *= require_self
    *= require 'owl.carousel.min.css'
    *= require 'owl.carousel.min.css'
    *= require 'owl.theme.min.css'
    *= require "flag-sprites.min.css"
    *= require 'owl.transitions.min.css'
    */

layout.html.erb:

<%= stylesheet_link_tag "application.css" %>

【问题讨论】:

  • 主要问题是什么?
  • 通过使用上面的代码,上面提到的css文件没有加载。
  • 删除 .css 并且不要使用 quot 和 require
  • 我从文件名中删除了引号和 .css,但没有帮助。我想要实现的是将多个文件作为一个文件提供,从而减少请求的数量。
  • 你在生产环境中测试过吗? rails s -e producation

标签: javascript css ruby-on-rails asset-pipeline


【解决方案1】:

如果你使用

*= require_tree .

它将加载 app/assets/stylesheet 中的所有 css 文件,因此无需提及每个文件名。

还有一点需要注意的是,在更新完 application.css 和 application.js 文件后需要重启服务器。希望对您有所帮助。

【讨论】:

  • 我不想加载整个列表,有超过 50 个特定于每个功能、控制器的 css 文件。我正在尝试优化代码。
  • 你可以参考link,有多种选择,你可以试试。
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 2012-03-06
  • 1970-01-01
  • 2011-07-27
相关资源
最近更新 更多