add external style sheet in rails project
I'm just going to assume you're already using something like this in your layout:
stylesheet_link_tag 'application'
If you want to refer to an external stylesheet then all you need to do is pass in the url.
stylesheet_link_tag 'application', 'http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css'
If you want to include the actual files in your codebase:
app/assets/stylesheets
is where you should place your main css files.
vendor/assets/stylesheets
is where you should place the css files for any plugins that you might have.
It's the same for js files. For example your application.js
would go in app/assets/javascripts
and a jquery plugin like timeago.js
would go in vendor/assets/javascripts
.
where to put the external css and js in rails
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 compress 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 "compile" 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.com/stylesheet.css" %>
How do I use CSS with a ruby on rails application?
Put the CSS files in public/stylesheets and then use:
<%= stylesheet_link_tag "filename" %>
to link to the stylesheet in your layouts or erb files in your views.
Similarly you put images in public/images and javascript files in public/javascripts.
Rails 6: How to associate specific stylesheet and external js plugin to a layout
With Asset Pipeline:
- Create a
special.js
file in theapp/assets/javascripts
folder. Add JS files to
special.js
//= require monnom
//= require mintymonCreate a
special.css(.scss)
file in theapp/assets/stylesheets
folder.Add CSS files to
special.css(.scss)
@import "monnom";
@import "mintymon";If it doesn't work like this, add a custom path into
config.assets.paths
so the autoloader can find them// assets.rb
config.assets.paths << Rails.root.join("vendor", "javascripts") // I think the folder name should be javascipts instead of javascript
config.assets.paths << Rails.root.join("vendor", "stylesheets")Then restart
rails server
You can see a list of
load paths
. UseRails.application.config.assets.paths
command inrails c
.
Not able to include external stylesheet in rails application
Your problem has the hallmarks of a Rails asset pipeline problem
- Do you have an application.css.erb with something like this? It's created by default and serves as the backbone for the CSS aspect of the asset pipeline in rails.
/* ...
*= require_self
*= require_tree .
*/
- Using the asset pipeline means that a call like "/stylesheets/style.css" would never be valid in production and should be avoided in code.
If you're seeing this problem in production, did you rake your assets to build the fingerprinted filenames?
The raked filenames are mangled to something like: /assets/style-4dd5b109ee3439da54f5bdfd78a80473.css, so you can see why using the filename "style.css" will not work.
# Rake assets for production
$bundle exec rake assets:precompile RAILS_ENV=production
Debug
To me step 1 is making sure your pipeline is setup to serve CSS assets. Until that works, your CSS will be broken. I would ask you to first check on the application.css.erb, to make sure your app includes your CSS tree in the pipeline. Then run the production rake, this will tell you what is happening with your CSS assets. If you post the output to your question, then we could see which, if any CSS is being included in your app.
Required reading for anyone doing Rails apps is the Asset Pipeline guide
Where do you put CSS files in a rails app directory?
for Rails 2.x : public/stylesheets
Related Topics
Stub Method Only on The First Call with Rspec
Ruby, How to Add a Param to an Url That You Don't Know If It Has Any Other Param Already
What the Purpose of Bind/Unbind Methods in Ruby
Gmail Threading, Imap and Ruby
What's the Best Way to Start a Background Process, That Can Get Accessed Later On
Howto: Model Scope for Todays Records
How to Use Present? in Ruby Projects
Superclass Mismatch, Struct, Reloading and Spork
How to Implement Composite Primary Keys in Rails
Normal Variables VS Instance Variable in Ruby, Whats the Difference
Accessing Headers from Sinatra
How to Find Out Why a Gem Bundle Has Locked a Gem at a Specific Version
When to Use Self in Module's Methods
How to Increment an Integer in Ruby
Rails Guides Offline Documentation
Why Does Using '-Webkit-Appearance: None' on a Select Option in Osx Make The Text Disappear