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:

Community
  • 1
  • 1
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • Can you go into more detail about why `stylesheet_link_tag` isn't working for you? – James Mar 10 '12 at 04: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. – hrishikeshp19 Mar 10 '12 at 04: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. – hrishikeshp19 Mar 10 '12 at 04:44
  • 2
    The word external is throwing people off I think, you just want to import .css files right? Have a look at http://guides.rubyonrails.org/asset_pipeline.html and http://stackoverflow.com/q/9138946/859762 – Ben Mar 10 '12 at 04:58
  • It's a dupe if the question is the same, not the answer. Can't speak to the downvote, it wasn't mine. – Michael Petrotta Mar 10 '12 at 05:33
  • @MichaelPetrotta: Ok, I agree its a dupe, but,the answer in the original question is not working, also, I did not receive any response when I pointed out that the answer is not working. So, now, my question is what am I supposed to do in this case. – hrishikeshp19 Mar 11 '12 at 20:32
  • The correct thing to have done in this situation would have been to post a [bounty](http://stackoverflow.com/faq#bounty) on the original question. – Michael Petrotta Mar 11 '12 at 21:16

2 Answers2

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.

James
  • 4,797
  • 25
  • 29
  • 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. – hrishikeshp19 Mar 10 '12 at 04:54
  • 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 – hrishikeshp19 Mar 10 '12 at 05:19
  • 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. – James Mar 10 '12 at 05:25
  • 1
    Hey.. after moving the actual file to vendor/assets/stylesheets, how can we refer to it in the view page?? ... Should I give relative path starting from root(public folder) to the stylesheet folder? or jus mention of filename is enough? – Aks.. Nov 18 '13 at 10:55
  • @akshatha you can just use the view helper: `stylesheet_link_tag :reporting` – James Nov 18 '13 at 22:01
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)

Arnaud
  • 101
  • 1
  • 13