How to write ruby ​​code in css file

I have a rails application in which I need a css file so that its property can be changed using ruby ​​code. ex. background_color: <% = ruby ​​code that returns the background color%>

Thus, the user can set his css property and will be applicable only to this user as a topic.

Thank!

+3
source share
5 answers

Check out this screencast that explains how to customize dynamic CSS files using ERB (Rails Template System) http://nubyonrails.com/system/images/css-erb-small.mov

0
source

/ . , . .

.active .inactive (). css , .. , .

? , , , ? - :

<body class='<%= "development" if Rails.env == 'development' %>'>

<body <%= "style='background-color: red;'" if Rails.env == 'development' %>

ruby ​​ CSS javascript, , , , , . , , .

:

<style>
    .user .name{
       color: <%= current_user.chosen_color %>;
    }
</style>

p.s. - .. javascript

p.s.2. : scss config.rb scss , , , .

+3
+2

- , :

<head>
....
<% unless current_user.theme.nil? %>
<style>
body{
  background:<%= current_user.theme.background_color%>;
}
</style>
</head>
<% end %>

, SASS - , , , , . , HTML .

. , .

+1

Check out this tutorial about using the SASS mechanism to dynamically change CSS.

0
source

Source: https://habr.com/ru/post/1755790/


All Articles