How can I access Rails objects in Sass?
In a Rails 3.1.0 project, I have Companies with a few customizable attributes like background_color and link_color. I want to be able to set some Sass variables like so:
$background_color: <%= @company.background_color %>
$link_color: <%= @company.link_color
...
This doesn't work because @company
is nil when Sass does its thing. I'm not sure how to go about solving th开发者_Go百科is in a way that's dynamic (companies can be created and colors can be changed and the views update immediately). Any suggestions?
I can think of a couple approaches off the top of my head:
- Serve your stylesheet through a controller.
- Use CSS classes to configure the colors and serve just that CSS through a controller, inlined partial, or a CSS
@import
.
Serving your stylesheet through a controller is pretty straightforward so there's not much to say. This might be a bit ugly and cumbersome.
For the second one, you'd add a couple extra CSS classes:
.custom-bg {
background-color: some-default-bg;
}
.link-fg {
color: some-default-fg;
}
/*...*/
Then any element that need to use the custom background color would need their usual CSS classes and custom-bg
; similar shenanigans would be needed for the other configurable values. To supply the customized CSS, you could inline a <style>
element into your HTML using a standard ERB partial or you could serve the CSS through a controller (either through <style src="...">
or @import
). So you'd fake the SASSy goodness with old school multiple CSS classes in your HTML.
There's also JavaScript. You'd need some way to identify the elements that need their colors adjusted and then adjust them directly with things like this:
$('.need-custom-background').css('background-color', '...');
I think you might be able to do something just like what you have there, but you need to change the extensions of the files to '.css.scss.erb'
To follow up on this, I did create a stylesheet controller but it was rather contrived to get Sass parsing and asset pipeline load paths all working correctly. I ended up dumping that and reorganizing the styles so I could generate a static stylesheet for each company which gets regenerated and uploaded to S3 on company update.
Well, if you mean a dynamic object like a model loaded via a controller, you can't really, at least not very easily. This is because unlike HTML ERB templates, the SASS ones are generally rendered once and served statically unless something changes in the code or they are re-precompiled via rake (depending on your environment configs). But you can access some helper methods, global objects, and use some ruby in there by renaming the file with an "erb" extension e.g. application.css.scss.erb
. See
- https://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
- How can I use Ruby/Rails variables inside Sass?)
If you need styles based on dynamically loaded objects, like models, you can...
- Write CSS styles literally in the template
- Compile the stylesheets dynamically. See the top-rated answer here: How do I create dynamic CSS in Rails?
- For some use cases you might accomplish the same thing by leveraging Rails/SASS's import path hierarchy (i.e. SASS
@import 'partial_name_with_no_path'
will search the importing SASS files folder first and then fall back to the top level - You can configure this as well).
精彩评论