简体   繁体   English

在Rails应用程序中使用javascript加载特定的CSS文件

[英]Load particular CSS file with javascript in a rails app

I'm using skeljs which loads css file according to the device width. 我正在使用skeljs根据设备宽度加载css文件。 How do I go about doing this in rails. 我该如何在Rails中做到这一点。 Where my css and javascript files are in their particular subdirectories in the app/assets folder and have specified the paths in the js file. 我的css和javascript文件位于app / assets文件夹中特定子目录中,并在js文件中指定了路径。 I'm using a template by html5up.net In the init.js file if set the href to each CSS file as '/assets/stylesheets/file.css' but it doesn't seem to work. 我正在html5up.net上使用模板,如果将每个CSS文件的href设置为“ /assets/stylesheets/file.css”,则在init.js文件中使用该模板,但它似乎不起作用。

If you are using rails 3.0 or above, then you can asset pipeline for storing your js/css files. 如果您使用的是Rails 3.0或更高版本,则可以使用资源管道来存储js / css文件。

You need to add <%= javascript_include_tag 'application' %> in your layout file for eg. 您需要在布局文件中添加<%= javascript_include_tag'application'%>,例如。 layouts/application.html.erb 布局/ application.html.erb

Above code will add application.js in your webpage, This file will be compressed and minimized in production mode. 上面的代码将在您的网页中添加application.js,此文件将在生产模式下压缩并最小化。

Now in application.js, you can include your different js file 现在在application.js中,您可以包含您的其他js文件

//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require util
//= require bootstrap
//= require sessions

Same thing can done for css files in application.css.scss 可以对application.css.scss中的CSS文件执行相同的操作

/*
 *= require_self
 * require_tree .
*/
@import "bootstrap";
@import "bootstrap/theme";
@import "users";

for more details about asset pipeline you can follow this link 有关资产管道的更多详细信息,请单击链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM