Render from your Model

Ok, with MVC anti-purism aside, how can I render an RXML template from a Model? I have a Model object which I want to serialize to XML. I originally had all my XML generation methods inside the Model object itself and that works just fine, but now I want to clean up the object and I think it would be best to put in an RXML template. But none of the render() set of methods work from a Model, only ActionController/ActionView.

Someone has blogged about this before at: http://blog.yanime.org/articles/2006/08/05/rails-calling-render-outside-your-controllers

But the problem with this approach is that you cannot assign variables and/or the template does not pick up any instance variables you have set in your model prior to the render() call.

So does anybody have any ideas? Or should I just go back to the stone-age and place my XML right in the Model?

thanks -william

Hi,

Why not just use to_xml() ? ( Rails 1.2 & Edge )

See ActiveRecord::Base
and http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/ Hash/Conversions.html

- Lourens

I cannot use to_xml() because my XML is not generic enough, its specific.

I was able to get that one Module to work, that is:

http://blog.yanime.org/articles/2006/08/05/rails-calling-render-outside-your-controllers

Thanks for all your ideas and help!

Sure you can.

active_record/lib/active_record/xml_serialization.rb

Straight from the docs:

     # To include any methods on the object(s) being called use :methods