I have the following code in one of my Rails 3 application views:
<ul>
<li style="position:relative">
<b>MR</b> - 18 Jun. 2012 - 11:07<br />
<p style="width:85%">Paid by Cheque 000562</p>
<span style="position:absolute;top:0px;right:0px;text-align:right">£0,00<br /><b>Balance: -£7,36</b></span>
</li>
<hr />
<li style="position:relative">
<b>JJ</b> - 01 May. 2012 - 09:35<br />
<p style="width:85%">label 2 Packets Recovery -Rabbit</p>
<span style="position:absolute;top:0px;right:0px;text-align:right">£0,00<br /><b>Balance: £7,36</b></span>
</li>
<hr />
This displays the content as follows:

The raw Rails code is as follows:
<ul>
<% @animal.clinicals.each do |clinical| %>
<li>
<b><%= clinical.UserID %></b> - <%= clinical.FullDateTime.strftime("%d %b. %Y - %H:%M") %><br />
<p><%= clinical.ClinicalText %></p>
<span><%= number_to_currency(clinical.Fees, :unit => "£", :separator => ",", :delimiter => "") %><br /><b>Balance: <%= number_to_currency(clinical.LineBalance, :unit => "£", :separator => ",", :delimiter => "") %></b></span>
</li>
<hr />
<% end %>
</ul>
What I am trying to do is alter the HTML code depending on the values in the ClinicalText field. For example, the following are possible entries to that field:
label 2 Packets Recovery -Rabbit
Surcharge (Credit)
disp 250 Tabs...
So, if the ClinicalText contains ‘label’ at the beginning I would like to remove the word label and add a css style to the li. Likewise, if it contains ‘Surcharge’ or ‘disp’ I would like to add different css styles to the li.
I can think of a way of doing this using if, else and elseif statements in the view but I would prefer this to be in a helper of some kind.
Any pointers would be appreciated!
I’d write a helper that generates the whole
<p>tag – that’ll make it easier for you to add classes, which you can then style with CSS. You can use acase(switchin most other languages) statement:Then this line:
Would become:
And that’s it! Note that those regexes are case insensitive, but require the trigger word to be the very first thing in the input string (no spaces or nuthin); also, only the
"label"case strips out the word it matches. That’s how I interpreted your question, but I could be wrong. Tweak as necessary.Hope that helps!