Jeff Burghess
Jeff Burghess

Reputation: 383

How does css in rails work?

I'm making my first website with rails. I'm a little confused with how rails does css files.

  1. How do you maintain separate styles for each page if rails combines all the css files in the stylesheets directory into application.css? For example, if I wanted to include extra css just for one page I can do that normally by just making a new css file and referencing it in html, but it seems that's not how rails does it. Is it bad/Does it work if I just do something like "<link rel="stylesheet"..." right in one of the views?
  2. I see that rails makes a new css file for every controller I do. Does this just mean the code put into these files will only used on views associated with the specific controller?

Upvotes: 0

Views: 51

Answers (1)

Ropeney
Ropeney

Reputation: 1065

So basically its been decided that its quicker to compile all the CSS for your whole site and then thats cached on the users browser than to serve every different pages CSS. So for each view, you put *= require viewname or if you have require_tree . in your application.css then it will autoload everything in that directory. The splitting of each CSS file is just purely for ease of development.

If you want to have seperate for each page then put stylesheet_link_tag "style" at the top of your view, and make sure it's not included in application.css. You will also need to add it to your asset compiling through:

# config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( cssfile.scss )

Upvotes: 2

Related Questions