开发者

Accessing Rails Models or Helpers in SCSS

开发者 https://www.devze.com 2023-03-13 21:40 出处:网络
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...

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

0

精彩评论

暂无评论...
验证码 换一张
取 消