In the default layout of application.html.erb that gets generated the book has added this line of code, saying because rails loads all of the stylesheets at once, we need a convention to limit controller-specific rules to pages associated with that controller. Using the controller_name as a class name is an easy way to do that..and they have added this code:
<body class = '<%= controller.controller_name %>' >
But still I don’t understand what is it doing? Can someone explain it ? and actually is it a common and recommended thing to do or not?
It’s a way to scope your css based on the controller.
Let’s say you have 2 models:
CarandBrand, and you want the top menu to be green on the page where you display all the cars, and red on the page where you display the brands.On the page where you list all the cars (cars#index), the body would look like that:
On the page where you list all the brands (brands#index), the body would look like that:
So you would be able to write your css like the following: