简体   繁体   中英

Ruby on Rails 4: How to include Javascript and CSS?

I'm new to rails and I'm building an application using rails 4. I want to create a page that uses JQuery, another 3rd party javascript library available via CDN, a 3rd party CSS library available via CDN, my own javascript, and my own CSS. I need to use the:

jQuery(function($) {
  ...
});

function to execute some javascript when the page is loaded and this only seems to work if I put the call to jQuery(function($)... in the of the document.

I want JQuery to be included in all my apps pages, but everything else I mentioned above I only want included in this particular page.

What should I do? I've been reading various things online and I'm feeling confused. Should I download the 3rd party resources and include them somewhere under the vendor directory? If I do that, how I can I make sure they're only included in that particular page? Isn't downloading 3rd party resources all about leveraging the asset pipeline to combine everything into one huge JS file so there are less HTTP requests? So, if I only want this 3rd party script included in one page, why would I want to leverage the asset pipeline? Should I still want to? Should I not want to have page specific javascript files? If so, how about the javascript that needs to be called to initialize that particular page (eg in order to allow for unobtrusive javascript) and shouldn't be called anywhere else?

Where do the javascript and css files that I have written go? How do I make sure they get included in the head of the document? Do I need to add a yield block in the head of my application layout in order to get this to happen

Someone please help me to learn the standard and best-practices way of doing this.

您可以通过将它添加到application.js来创建common.js并将其包含在资产管道中,此外,如果您不使用require_tree ,则必须使用jQuery(function($)分别包含文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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