patternrubyrailsMinor
Setting bgcolor in Ruby. What's the proper way to do it?
Viewed 0 times
bgcolorthewhatwaysettingrubyproper
Problem
;" >be_color is a method of Debate that returns a string like 45FFFF.Although it does what I want, this seems like a tremendously bad idea. What would be a better way to accomplish this? (I'm very new to both Ruby and Rails. And new-ish to web development in general.)
Solution
Based on your restrictions I would say this solution is fine. If you don't want to do inline styles though you could definitely consider something like either writing the color to a javascript variable or a data-* attributes on the debate element.
or the data solution:
But if it were me I would actually do the color calculation on the frontend, meaning you would basically assign the number of actions for each debate to javascript variables, and what would be done with those (a custom background color for instance, or anything else) would be determined in javascript. The color of any element just doesn't seem like something you would want to calculate on the server side unless it involves complex business logic.
var debateColor = '';
or the data solution:
';But if it were me I would actually do the color calculation on the frontend, meaning you would basically assign the number of actions for each debate to javascript variables, and what would be done with those (a custom background color for instance, or anything else) would be determined in javascript. The color of any element just doesn't seem like something you would want to calculate on the server side unless it involves complex business logic.
Code Snippets
<script type="text/javascript">
var debateColor = '<%= debate.bg_color %>';
</script><p class="debate" data-debate-color="<%= debate.bg_color %>';Context
StackExchange Code Review Q#2777, answer score: 2
Revisions (0)
No revisions yet.