# How to associate a model's attribute with CSS style

**URL:** <https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855>\
**Category:** Rails\
**Created:** [December 31, 2006, 9:01am UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855 "2006-12-31T09:01:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2006, 9:01am UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855/1 "2006-12-31T09:01:53Z")

</div>

So this isn’t a question specific to Rails, but I wanted to keep the  
purity of the MVC architecture as much as possible.

I have an AR model, order, which can have a status. I want to style  
the orders based on what their status is. Right now all my status AR  
model has is a name and I’m creating the CSS class in my view with  
something like:

class="\<%= order.status.name %\>"

but I don’t like how coupled this makes everything. I’m sure people  
have figured out a best practice for this. I was thinking maybe adding  
a style attribute to Status? Anyone else have a better suggestion?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2006, 10:22am UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855/2 "2006-12-31T10:22:05Z")

</div>

I don’t see how this is incorrectly coupled, you are printing out the  
status name in the view which the view then styles accordingly. Adding  
a style attribute to the model would be incorrect because then the  
model would know about the view.

Since the styling only makes sense in this particular html view (if  
you were to render as xml it would be unnecessary) then that seems  
like the best place to me.

On 12/31/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but I don’t like how coupled this makes everything. I’m sure people  
> have figured out a best practice for this. I was thinking maybe adding  
> a style attribute to Status? Anyone else have a better suggestion?
> 
> Thanks,  
> Joe
> 
> >

–  
Ross R.  
[www.sorrylies.com](http://www.sorrylies.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855/3 "2006-12-31T14:22:35Z")

</div>

I don’t see this as a violation of MVC at all. In fact, I’d say that  
adding a style attribute to status _would_ be a violation.

One thing i would say is that it’s probably worth making a helper to  
get the css class:

def css\_style\_from(text)  
text.tablelize  
end

class=“\<%= css\_style\_from(order.status.name) %\>”

or something like that. Tablelizing the text will mean you don’t get  
problems with spaces in status names:  
Order Complete becomes order\_complete

It’s not great to use tablelize in the view as you may change your CSS  
naming rules, so a nice little helper will keep you DRY.

Hope that helps,

Steve  
[www.curve21.com](http://www.curve21.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855/4 "2006-12-31T23:40:10Z")

</div>

Yeah, I figured out early on that having spaces in a style is not  
possible. The only thing I’m worried about is that if I make a style  
for “pending” it will be a programming change (changing the style’s  
name) if the user’s ever want to change what the name is, “unprocessed”  
for example. Not a big deal, but I never like having to make  
programming changes when an arbitrary value is changed. Thoughts?  
Thanks for the help!

Joe

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-associate-a-models-attribute-with-css-style/80855/5 "2007-01-02T12:09:17Z")

</div>

> Not a big deal, but I never like having to make  
> programming changes when an arbitrary value is changed. Thoughts?

I’d say that unless the client _needs_ to update the status name  
regularly, it’s unnecessary to provide them with that functionality. I  
guess it comes down to the system in the end. If this is a system  
where the styles and names are changed regularly, then storing styling  
in the db might not be such a bad thing. However, in most cases it’s  
probably not needed and you should choose the method that makes your  
life easier and benefits the application.

Steve
