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 f开发者_运维技巧rom the controller's data.
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
.
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
精彩评论