Rails 4: how do I use Sass Mappings?

2019-01-08 02:01发布

问题:

I'm trying to cleanup some of my styles for a Rails 4.1 app. Instead of listing out every color and it's variations, I would like to create a mapping of colors/values and then generate the CSS by iterating over it.

# colors.css.scss
$colors: (
  "red": #330000,
  "blue": #003300
);

However, I get the following error from this:

Invalid CSS after "$colors: (red": expected ")", was ": #330000, blue..."

I can use Sass variables and mixins just fine; why can't I use SASS mappings as well?

回答1:

As @dinocarl kindly pointed out, the map and list features of SASS are in version 3+. I had to upgrade sass-rails gem to version 5+ because that version of the gem packages SASS 3+. Worked like a charm after I upgraded the gem version (also were few additional dependencies I had to update).