【问题标题】:Rails 4 Assets Managment for Stylesheets/Js/Images样式表/Js/图像的 Rails 4 资产管理
【发布时间】:2014-06-20 13:37:54
【问题描述】:

我使用的是管理主题,它有 10-15 个 CSS/JS 文件和更多图片。

我想包含它们并在新模板 admin.html.erb 中使用它们

包含它们的最佳方法是保持文件夹完整。

除了application.css 之外,我可以使用指令创建另一个名为admin.css 的清单文件吗?

这是主题导入资产的方式

【问题讨论】:

标签: javascript css ruby-on-rails image


【解决方案1】:

有几件事需要用这个来解决。首先,删除那个巨大的红色和蓝色斑点。其次,将“字体”目录移到资产目录中。然后,弹出你的 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_tree .
*/

@font-face {
    font-family: 'FontAwesome';
    src: url('fontawesome.eot');
    src: url('fontawesome.eot?#iefix') format('embedded-opentype'), url('fontawesome.svg#fontawesome') format('svg'), url('fontawesome.woff') format('woff'), url('fontawesome.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

这将自动将“样式表”目录中的所有样式表加载到您的应用程序中,并注册您的字体以便 css 可以使用它。第三,弹出你的 application.js 文件并确保它看起来像这样:

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

Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.

It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
compiled file.

Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
about supported directives.

= require jquery
= require jquery_ujs
= require turbolinks
= require_tree .
*/

“require_tree”语句的作用与 application.css 中相同的语句完全相同;将所有内容加载到应用程序中。现在您可以在视图中调用 JavaScript!

希望这会有所帮助!

【讨论】:

  • 我有管理员的名称范围,所有这些都是管理员相关的资产。我将为网站的前端提供类似的资产。那么我如何使用诸如 application.js 文件之类的东西将它们分开
  • 我建议在文件维护方面遵循标准的 Rails 结构,因为这样更容易,而且这个答案就是这样工作的。其次,您不必担心名称范围和所有这些。 JS 文件在运行时都会被严重混淆,所以你不必担心。
  • 为什么我要在前端提供管理部分资产?用户下载的数据太多。我说的是 100-150kb 的管理 js/css 文件
  • 就像我说的,Rails 会在运行时压缩和缩小它的所有资产,除此之外,用户不会“下载”任何东西。我认为您需要更多地研究 Rails 和 Rails 资产管道。此外,根据您喜欢的目录结构,您可能需要查看 node.js:nodejs.org
  • 不,我得到了关于缩小的部分。当我包含 application.js 时,它将导入所有其他文件并缩小它。这是我不想要的。
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多