javascript - 用于样式表/Js/图像的 Rails 4 Assets 管理

标签 javascript css ruby-on-rails image

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

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

enter image description here enter image description here

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

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

这是主题导入 Assets 的方式

enter image description here

最佳答案

有几件事需要用这个来解决。首先,删除那个巨大的红色和蓝色 Blob 。其次,将您的“字体”目录移到您的 Assets 目录中。然后,弹出您的 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;
}

这样做会自动将“stylesheets”目录中的所有样式表加载到您的应用程序中,并注册您的字体以便 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 中的相同语句完全相同;将所有内容加载到应用程序中。现在您可以在 View 中调用 JavaScript!

希望这对您有所帮助!

关于javascript - 用于样式表/Js/图像的 Rails 4 Assets 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24328615/

相关文章:

c# - Javascript 函数中复选框的值

javascript - 如何使用 EaselJS 在位图上绘制/绘制线条?

javascript - 我尝试在打开模式之前验证我的表单

css - 使用 CSS 对齐表格中的内容

ruby-on-rails - rails : Using distinct with where

ruby-on-rails - rails apartment gem 将数据从一个租户复制到另一个租户

javascript - 如何在 recaptcha 回调中访问更新的 props

css - GWT CssResource 中的基本算法

css - 如何强制 Chrome 使用 `box-sizing: content-box` ? (2 个嵌套 DIV,外部显示为 `table-cell` 。)

mysql - 如果修改了源则更新表