Rails Sass: variables are not passed with @import

半腔热情 提交于 2021-02-20 06:49:10

问题


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.


回答1:


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




回答2:


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




回答3:


@import is incompatible with = require. Replace and remove any = require even if you think you've commented it out.



来源:https://stackoverflow.com/questions/30187407/rails-sass-variables-are-not-passed-with-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!