16

I am using Rails 3.2.1. How do I add external style sheets in my rails application?

I've tried the following answers to no avail:

7
  • Can you go into more detail about why stylesheet_link_tag isn't working for you? Commented Mar 10, 2012 at 4:05
  • 1
    @MichaelPetrotta: Off course it is. I have given link to the same in my question, but the answer for that question is not working. I hope it is valid reason to post the question again, and does not deserve a sownvote. Commented Mar 10, 2012 at 4:42
  • @James: Well, I have not tried stylesheet_link_tag because I have not added the stylesheets yet because I do not know exactly where i am supposed to put stylesheet files. Commented Mar 10, 2012 at 4:44
  • 2
    The word external is throwing people off I think, you just want to import .css files right? Have a look at guides.rubyonrails.org/asset_pipeline.html and stackoverflow.com/q/9138946/859762 Commented Mar 10, 2012 at 4:58
  • It's a dupe if the question is the same, not the answer. Can't speak to the downvote, it wasn't mine. Commented Mar 10, 2012 at 5:33

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. Actually my question was "at what path" in my application directory should I put my js and css files? Later I can use stylesheet_link_tag.
Phew...now its much more clear...thanks a lot. One last thing, do we need to put plugins in "vendor"? If yes why? If no..what is advantage/reason for putting my plugins into vendor directory
There isn't an actual requirement for putting plugins into the vendor directory; it's just to keep your javascripts organized. For example: the timeago.js file for the timeago plugin is most likely minified and you probably won't be modifying it all. It should be in a different directory from the custom javascripts that you're actively working on.
Hey.. after moving the actual file to vendor/assets/stylesheets, how can we refer to it in the view page?? <link href="reportng.css" rel="stylesheet" type="text/css" />... Should I give relative path starting from root(public folder) to the stylesheet folder? or jus mention of filename is enough?
@akshatha you can just use the view helper: stylesheet_link_tag :reporting
1

After looking for a solution to this problem for a while, I found this step by step tutorial which explain how to implement a bootstrap theme to a rails app => http://www.notch8.com/implementing-a-bootstrap-theme/

You can apply this to other CSS and JS templates than a Bootstrap one.

Hope this help (sorry if I make english mistakes)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.