简体   繁体   English

Rails资产管道

[英]Rails Asset Pipeline

I have 2 questions. 我有两个问题。

  • In my Rails app that I'm practicing with I have my file tree like below: 在我正在练习的Rails应用中,我的文件树如下所示:

在此处输入图片说明

Learning from Michael Hartl's tutorials I learned to add the "custom.css.scss" file to take care of CSS. 从Michael Hartl的教程中学到的知识,我学会了添加“ custom.css.scss”文件来处理CSS。 My question is WHY don't the CSS stuff go into "kalendar.css.scss" which picks it's name from my "Kalendar" controller or why doesn't the CSS go into "application.css"? 我的问题是,为什么CSS内容不放入“ kalendar.css.scss”(它从我的“ Kalendar”控制器中选择名称),或者CSS为什么不归入“ application.css”呢?

  • With javascript, would I need to create a "custom.js.coffee" file? 使用javascript,是否需要创建“ custom.js.coffee”文件? If not, how would I want to add a text effect on a text that will be shown in the view. 如果不是,我想如何在视图中显示的文本上添加文本效果。 Meaning how can I add an extra javascript plugin which I download? 意味着如何添加我下载的额外的javascript插件?

But I know in the view i can do something like: 但我知道在视图中我可以执行以下操作:

<%= javascript_tag "alert('hey you!')" %>

EDIT: 编辑:

Now in my "vendor/assets/javascripts/textEffect.js" I have this code which is a plugin I download online. 现在,在我的“ vendor / assets / javascripts / textEffect.js”中,我有这段代码,这是我在线下载的插件。

(function($) {

    function shuffle(a) {
        var i = a.length, j;
        while (i) {
            var j = Math.floor((i--) * Math.random());
            var t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }

    function randomAlphaNum() {
        var rnd = Math.floor(Math.random() * 62);
        if (rnd >= 52) return String.fromCharCode(rnd - 4);
        else if (rnd >= 26) return String.fromCharCode(rnd + 71);
        else return String.fromCharCode(rnd + 65);
    }

    $.fn.rot13 = function() {
        this.each(function() {
            $(this).text($(this).text().replace(/[a-z0-9]/ig, function(chr) {
                var cc = chr.charCodeAt(0);
                if (cc >= 65 && cc <= 90) cc = 65 + ((cc - 52) % 26);
                else if (cc >= 97 && cc <= 122) cc = 97 + ((cc - 84) % 26);
                else if (cc >= 48 && cc <= 57) cc = 48 + ((cc - 43) % 10);
                return String.fromCharCode(cc);
            }));
        });
        return this;
    };

    $.fn.scrambledWriter = function() {
        this.each(function() {
            var $ele = $(this), str = $ele.text(), progress = 0, replace = /[^\s]/g,
                random = randomAlphaNum, inc = 3;
            $ele.text('');
            var timer = setInterval(function() {
                $ele.text(str.substring(0, progress) + str.substring(progress, str.length).replace(replace, random));
                progress += inc
                if (progress >= str.length + inc) clearInterval(timer);
            }, 100);
        });
        return this;
    };

    $.fn.typewriter = function() {
        this.each(function() {
            var $ele = $(this), str = $ele.text(), progress = 0;
            $ele.text('');
            var timer = setInterval(function() {
                $ele.text(str.substring(0, progress++) + (progress & 1 ? '_' : ''));
                if (progress >= str.length) clearInterval(timer);
            }, 100);
        });
        return this;
    };

    $.fn.unscramble = function() {
        this.each(function() {
            var $ele = $(this), str = $ele.text(), replace = /[^\s]/,
                state = [], choose = [], reveal = 25, random = randomAlphaNum;

            for (var i = 0; i < str.length; i++) {
                if (str[i].match(replace)) {
                    state.push(random());
                    choose.push(i);
                } else {
                    state.push(str[i]);
                }
            }

            shuffle(choose);
            $ele.text(state.join(''));

            var timer = setInterval(function() {
                var i, r = reveal;
                while (r-- && choose.length) {
                    i = choose.pop();
                    state[i] = str[i];
                }
                for (i = 0; i < choose.length; i++) state[choose[i]] = random();
                $ele.text(state.join(''));
                if (choose.length == 0) clearInterval(timer);
            }, 100);
        });
        return this;
    };

})(jQuery);

And in my "View" I have a file with content: 在我的“视图”中,我有一个包含内容的文件:

<p class="my-container">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

in my "application.js" I have: 在我的“ application.js”中,我有:

//= require textEffect

and in "app/assets/javascripts/custom.js.coffee" I have 在“ app / assets / javascripts / custom.js.coffee”中,

$(document).ready() -> 
$("#my-container").unscramble();

Unless, of course I'm wrong, I'm presuming on page load, I should see the text effect start up. 除非我当然错了,否则我假设页面加载正常,那么我应该会看到文本效果启动。 But Nothing happens. 但是什么也没发生。 Is there something I missed or doing wrong? 是否有我错过或做错的事情?

Apparently michael is just using this custom.css.scss file as an example. 显然,michael只是以此custom.css.scss文件为例。 You can use whatever file name is best for organizing your project. 您可以使用最适合组织项目的任何文件名。

For example I like to put layout related items in layout.css.scss , and I put Kalendar related css in kalendar.css.scss . 例如,我想将与布局相关的项目放在layout.css.scss ,并将与Kalendar相关的css放在kalendar.css.scss The only reason why I wouldn't use application.css for your custom styles, is because application.css is a manifest file. 不会为您的自定义样式使用application.css的唯一原因是,因为application.css清单文件。

A manifest file is a file that tells rails which assets to include using the comma syntax. 清单文件是使用逗号语法告诉rails包括哪些资产的文件。 For example, in css manifest 例如,在CSS清单中

/* Manifest File
 *= require_tree .
*/

This tells rails to include the whole directory of stylesheets. 这告诉Rails包括样式表的整个目录。 You could manually include files with 您可以手动添加文件

*= require layout
*= require my_file_name

The point of this system is to render all of the assets into one file, this makes the page load faster, and in production you should only need one css/js file. 该系统的目的是将所有资产呈现到一个文件中,这使页面加载速度更快,并且在生产中,您只需要一个css / js文件。 To include a manifest file you just write 要包含清单文件,您只需编写

= stylesheet_link_tag 'manifest_file_name'

File names are purely for organization. 文件名仅用于组织。

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

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