skip to Main Content

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


  1. Try removing *= require_tree from your application.css.scss. Using require 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

    Login or Signup to reply.
  2. Noting above, I’ve recently had some experience with assets not working as expected. The recommendation I received was to use:

    rake assets:clobber
    

    This will clean up the pipeline.

    https://github.com/rails/sprockets-rails/blob/master/README.md

    Login or Signup to reply.
  3. @import is incompatible with = require. Replace and remove any = require even if you think you’ve commented it out.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search