I have a rails project which uses twitter bootstrap and sass. The scss files are structured into folders so I have
a better overview. Now I want to define a file for global variables which contains my colors etc. and pass those values down to other files so I have less redundant code. While all code is properly imported and applied,
variables don’t work.
Here is the current setup:
stylesheets/application.css.scss
/*
*= require_self
*= require_tree
*/
/*
stylesheets/
|
|– base/
| |– _reset.scss # Reset/normalize
| |– _typography.scss # Typography rules
|
|– components/
| |– _buttons.scss # Buttons
| |– _messages.scss # Nachrichten
| |– _dropdown.scss # Dropdown
|
|– helpers/
| |– _globals.scss # Sass Variables
| |– _functions.scss # Sass Functions
| |– _mixins.scss # Sass Mixins
| |– _helpers.scss # Class & placeholders helpers
//more files omitted
|
|– vendors/
| |– _bootstrap_and_overrides.css # Bootstrap
| |– _scaffolds.scss # Bootstrap
|
|
`– main.scss # primary Sass file
*/
I’m not using the =require method as it does not allow the use of variables and mixins (which I’d like to use).
I also use a main.scss which contains all the imports.
stylesheets/main.scss
@import "bootstrap-sprockets";
@import "bootstrap";
@import "helpers/globals";
@import "base/normalize";
@import "base/grid";
@import "base/typography";
@import "components/buttons";
@import "components/tables";
//other files omitted
The helpers/globals.scss contains the color definitions:
stylesheets/helpers/globals.scss
$background-light : #4e4d4a;
The file component/tables.scss is supposed to use that variable.
.table-striped > tbody > tr:nth-child(2n+1) > {
tr, td, th {
background-color: $background-light;
}
}
According to most information on the web and the official SASS-guide this should work as I declared the variable and import the according file before the file that uses it. Certainly, the variable is not found:
Undefined variable: "$background-light"
.
The whole procedure seems rather simple but I’m running out of ideas. Do I need to set something in my environment files or do I need to change my application.css.scss? Or might bootstrap interfere here?
Thanks in advance for your help.
3
Answers
Try removing
*= require_tree
from yourapplication.css.scss
. Usingrequire
doesn’t work well with sass files, especially when combined with@import
.Don’t forget to import/require your
main.scss
file when you remove require_tree.https://github.com/rails/sass-rails#important-note
Noting above, I’ve recently had some experience with assets not working as expected. The recommendation I received was to use:
This will clean up the pipeline.
https://github.com/rails/sprockets-rails/blob/master/README.md
@import
is incompatible with= require
. Replace and remove any= require
even if you think you’ve commented it out.