I am trying to teach my models how to render themselves, i.e.
<%= my_model_object.render() %>
Let me explain my reasoning and proposed method before this gets shot
down as anti-MVC.
Let’s say I am writing a contact-management application. I have a
class Contact. I will need to display this class all over the
application. My first choice is to use a partial.
Now I can render my Contact with:
<% render_partial ‘contact_view’, :contact => my_contact %>
However, I start doing some ajax things, where I need to render a
contact and return it with an AJAX call. So, I simply render the
partial from my controller. Not too bad, as long as I can keep
straight what partial to call and what locals I need to pass where.
However, to make things a little easier on myself, I can create a
helper method in the controller like this:
helper_method :render_contact
def render_contact(contact)
render_partial(‘contact_view’, :contact => my_contact)
end
I can now call this same function from my view to render the page
initially, and from my controller to render new contacts for AJAX calls.
The next step is where I’ve run into trouble. Instead of having all
of these methods in the controller and calling out to them whenever I
need a Contact rendered, how about sticking the methods right onto
the model itself? Of course this breaks MVC if I put it on the model
in the contact.rb file, the contact is no longer reusable in other
places. But with ruby I’m supposed to be able to dynamically extend
objects. So why not extend my contact in my controller, teaching it
to render itself? In this way, different controllers could have
different rendering code for a Contact, and the Contact class itself
is never altered, but the code on my page goes from:
<%= render_contact contact %>
to
<%= contact.render() %>
and the controller AJAX code is likewise simplified.
So that’s the what and why, but for the life of me, I cannot figure
out the how. I am fairly new to ruby, and I think I’m supposed to be
able to do something like
def Contact
def render()
# render code goes here
end
end
inside of the controller, but I cannot for the life of me to get it
to work. If anyone call tell me how to do something like this, or if
there’s some alternative method that’s just as clean, I’m all ears.
Sorry for the longwinded message, but I felt that I should try to
explain myself as clearly as possibly with something that looks on
the surface to be a blatant breach of MVC.
In case I haven’t been clear, or maybe just because it’s in my head
and it’s late, here’s a metaphor I came up with:
Imagine paratroopers getting ready to jump out of a plane. As they
file towards the front of the plane, the officer hands them a
parachute, which they then put on and jump out. On the way down they
pull their own cord and parachute to safety. This is what I am trying
to do, with the model objects being the soldiers, the officer being
the controller, and the sky being the view. The method which I am
trying to avoid is that of pushing the soldiers out of the plane, and
then sending a parachute down after them.
Thanks for even reading this far, if you have,
Ozzi