2

I have installed the less-rails gem as I am keen to use the colour manipulation LESS offers. I need to extract a colour from my database as my themes base colour, and build up from there.

I have the static CSS, and have renamed it styles.css.less to ensure that rails understands the less extension, which it appears to.

The next thing I tried was to also wrap the file as an erb, to hopefully allow ruby string literals to process before being sent to LESS, and eventually outputting as valid CSS (still with me?)

The file is now called style.css.less.erb. While the file simple contains valid CSS, the processing of the document works. As soon as I add a ruby string literal, it fails.

color: #{"#112233"};

In the chrome debugger, nothing after this line is getting processed.

What am I doing wrong, and how should I do what I am trying to do?

2
  • 1
    That's not erb format. You'd want <%= "#112233" %>. But I don't think you're going to be able to pull a value out the database into your CSS; I'm pretty sure the asset pipeline doesn't have access to ActiveRecord. Commented Mar 22, 2012 at 10:21
  • seems this is the way to go for the second part of the journey -> stackoverflow.com/questions/6266129/from-db-to-css-file Commented Mar 22, 2012 at 10:32

1 Answer 1

1

As Chowlett says in comments, you should use erb syntax: <%= "#112233" %> Next step is get that value from db. If this color value is application-wide, probably you are looking for settings in db solution. I use rails-settings-cached gem for that. Your result code will looks like

color: <%= Setting.foo_color %>

If you are using assets on production, don't forget to recompile them after each setting change.

And if it's not a setting but probably something specific to each user then you can't use application-wide css files for that, but you can write inline css in views.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.