Uneasy conditionals in views?

I have many such conditionals in my views: ... <li id="context_<%= context.id %>" class="context_list_element <%= context.minimized ? 'minimized' : 'maximized' %>"> ... <div id="context_content<%= context.id %>"   <%= "style='display:none;'" if (context.minimized) %>> ...

I am specifically referring to the conditionals in the above code. They don't look very bad. But still, Something is definitely off, this can look better. But how?

would love to hear some comments, how do you guys handle such cases in code?

thanks. RoR rocks!!!

Jigar Gosar wrote:

I have many such conditionals in my views: ... <li id="context_<%= context.id %>" class="context_list_element <%= context.minimized ? 'minimized' : 'maximized' %>">

The ones that control business logic should refactor into the models, and the ones that control view logic (such as a style= tag) should refactor into helpers.

Next, for larger extents of non-DRY RHTML code, use partials.

<div id="context_content<%= context.id %>"   <%= "style='display:none;'" if (context.minimized) %>>

Going in the other direction, feel free to put the <%=%> _inside_ the style=''. Then it's safely empty if it's nothing.

Also always follow a perfect "symetry" rule. You don't break it here, but abusing partials and <%=%> might lead to that. Always have the < in the same code region as its >, the same for " and " on both sides of an attribute value, and balanced tags like <div> and </div>. Don't move one of those in a partial or something if you can help it!

And test the snot out of your views with assert_select and assert_ypath. The former gives a coarse but complete view of your HTML, and the latter is as precise as advanced XPath statements can get.

And test the snot out of your views with assert_select and assert_ypath.

Uh, assert_xpath?

assert_ypath's time is not yet come! (-: