How to set a variable from a helper method for inc

2019-02-11 02:26发布

问题:

I have a helper method that retrieves a string that I need to get into a SASS scss stylesheet. If I call the method in the view it works fine, however it seems that sass is wanting a variable. It seems as though if I attempt to set a var in the controller, thinking that this would be the correct approach initially, I'm not able to see the var anywhere.

I initially thought you could simply call a helper method from SASS, but apparently that's not the case? If not,

Could you please provide an example of getting a variable set properly in a sass scss stylesheet that would originally come from a helper method?

回答1:

Rename your SASS file to foobar.css.scss.erb. Then include the helper module you need that contains the helper method by placing this line at the top:

<% environment.context_class.instance_eval { include MyHelper } %>

Then you can use the helper method with erb

.foo {
    color: <%= cool_color %>
}