最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - where to put the external css and js in rails - Stack Overflow

programmeradmin1浏览0评论

I am just starting in rails and loading external resources like css and js has been an issue for me. Reading in few places, i found out that, css should be placed in app/assets/stylesheets and js inside app/assets/javascripts. After doing this i called my css and js in my view file like

<%= stylesheet_link_tag "<<path to my css>>" %> 

for css and

<%= javascript_include_tag "<<path to my js>>"  %>

and i included this line inside my config/initializers/assets.rb

Rails.application.config.assets.prepile += [/.*\.js/,/.*\.css/]

but doing this gave me some sort of pilation error. But i am not sure whether its the correct way or not to load external resource.Where are the other place that i need to change in order to load css and js and the best practice to include external resources in rails in terms of performance as well.

I am just starting in rails and loading external resources like css and js has been an issue for me. Reading in few places, i found out that, css should be placed in app/assets/stylesheets and js inside app/assets/javascripts. After doing this i called my css and js in my view file like

<%= stylesheet_link_tag "<<path to my css>>" %> 

for css and

<%= javascript_include_tag "<<path to my js>>"  %>

and i included this line inside my config/initializers/assets.rb

Rails.application.config.assets.prepile += [/.*\.js/,/.*\.css/]

but doing this gave me some sort of pilation error. But i am not sure whether its the correct way or not to load external resource.Where are the other place that i need to change in order to load css and js and the best practice to include external resources in rails in terms of performance as well.

Share Improve this question asked Jan 11, 2016 at 4:33 SidSid 5,8433 gold badges37 silver badges51 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Asset Pipeline

What you're referring to is something called the asset pipeline - the app/assets folder is where you store all the "dependent" files for your HTML -- css / js / images etc.

The asset pipeline is very simple -

The asset pipeline provides a framework to concatenate and minify or press JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB.

It's function is to provide you with a way to "pile" your CSS/JS into condensed (minified) files, which you can call in your front-end HTML. The ultimate aim is to make your "assets" as small as possible, so your page loads fastest.

--

In your case, you'll want to look up Sprockets Manifest Directives --

#app/assets/stylesheets/application.css
/*
   *= require self
   *= require_tree .
*/

The above will take every CSS file in app/assets/stylesheets and concatenate them into a single application.css file:

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag :application %>

So to answer your question directly, you only need to store external stylesheets in your app/assets/stylesheets folder.

If you have a "real" external stylesheet (hosted by Google or something), you'll want to include it in your layout as follows:

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag :application, "http://cdn.google./stylesheet.css" %>

Once you add your css and js in assests respective folder, you have to require those files in application.js and application.css and then you can include these two files in your html. Try following code:

application.css

//= require abc

application.js

//= require xyz

In your html:

<%= stylesheet_link_tag "application" %> 
<%= javascript_include_tag "application"  %>
发布评论

评论列表(0)

  1. 暂无评论