Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Rails Models or Helpers in SCSS

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

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

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

like image 554
Drew Avatar asked Jun 16 '11 14:06

Drew


2 Answers

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

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

So how could you ever possibly access variables like current_user from that .scss.erb file?

In my opinion, it's not possible to get controller variables in .scss.erb or .coffee.erb.

like image 124
Minqi Pan Avatar answered Oct 16 '22 01:10

Minqi Pan


You can chain template processors with Rails 3.1, so you can do my.css.scss.erb, and then embed your variables like so:

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

Then you can use the Sass variables throughout your SCSS.

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

like image 7
coreyward Avatar answered Oct 16 '22 01:10

coreyward