Models and HTML output

0 views
Skip to first unread message

Cyrille

unread,
Oct 23, 2007, 9:55:01 PM10/23/07
to WellRailed
Hi there,

I've got a philosophical question :-)

At the moment, I've got an Item class that has a method full_title. I
wanted to centralize a method full_title_with_link that would spit out
HTML like:

def full_title_with_link
"<a href='#{url}'>#{full_title}</a>"
end

Is it morally acceptable to have methods in models that returns HTML?

Another case that I have is a series of Action classes that inherits
from a base Action. They all include a description method that
describes the action as HTML. So quite conveniently I can do:

<ul>
<% @actions.each do |action| -%>
<li><%= action.description %></li>
<% end -%>
</ul>

Cheers

Cyrille

Orion Edwards

unread,
Oct 23, 2007, 10:30:35 PM10/23/07
to WellR...@googlegroups.com
/me ponders

Would you put SQL in your views? It's the same thing. Anyone for a spot of
PHP?
Also, why do your models store their own URLs? What happens if you change
domains or restructure your code so the URL is different?

You could easily just make a helper method and use that.
Future maintainers of your code will thank you for sticking to conventions.

As I see it the idea behind the whole MVC thing (and good programming
practice in general) is separation of concerns.
Hence you use models as 'data sources', views (and helpers) for
'presentation' and controllers to tie it all together.

I'm not idealistic enough to try tell you to outright ban HTML in models
forever, but I think it's bound to cause you pain if you do, so be aware of
that and try avoid it if you can.

* Escape Clause: If you're writing a CMS or something whose purpose is to
store HTML in database tables, the rules are different :-)

Cyrille

unread,
Oct 23, 2007, 11:41:03 PM10/23/07
to WellRailed
Agreed for the first one: I've put it in a helper.

But what about the second one, though? I want to inherit the HTML
output by default, with the possibility to override it. Object-
oriented helpers, anyone?

Cyrille

On Oct 24, 3:30 pm, "Orion Edwards" <orion.edwa...@open2view.com>
wrote:

Reply all
Reply to author
Forward
0 new messages