Access to Rails Models or Helpers in SCSS

I am using Rails 3.1 and SCSS in Asset Pipeline. Is there anyway access to the Rails helpers or controller data in the SCSS file? Something like...

#main { background-color: #{current_user.preferences.background_color} } 

I know that I can set my own $variables , but I'm not sure how I will populate them from the controller data.

+7
source share
2 answers

You can associate template processors with Rails 3.1 so that you can do my.css.scss.erb and then insert your variables as follows:

 $user-background-color: <%= current_user.preferences.background_color %> 

You can then use Sass variables throughout your SCSS.

I took a different approach to solving this problem for Rails 3.0: Using SASS with custom colors

+7
source

As far as I know, this is not what Asset Pipeline was designed for.

Think about it, you have the rake assets:precompile to convert all your .scss.erb files to a static .css file.

So, how could you access variables like current_user from this .scss.erb file?

In my opinion, it is not possible to get control variables in .scss.erb or .coffee.erb .

+8
source

All Articles