# Render from your Model

**URL:** <https://www.ruby-forum.com/t/render-from-your-model/79049>\
**Category:** Rails\
**Created:** [December 11, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049 "2006-12-11T21:31:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [December 11, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049/1 "2006-12-11T21:31:43Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [December 11, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049/2 "2006-12-11T21:46:26Z")

</div>

Your best bet is probably to build a generic controller to serialize a  
given model to XML. That way you don’t have to break MVC and your code  
is nice and clean.

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [December 11, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049/3 "2006-12-11T22:03:51Z")

</div>

Hi,

Why not just use to\_xml() ? ( Rails 1.2 & Edge )

See [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000934)  
and [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/)  
Hash/Conversions.html

- Lourens

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [December 11, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049/4 "2006-12-11T22:19:54Z")

</div>

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
 #
 # firm.to_xml :methods =>

```

[:calculated\_earnings, :real\_earnings]  
#  
#   
# # … normal attributes as shown above …  
# 100000000000000000  
# 5  
#   
#  
# To call any Proc’s on the object(s) use :procs. The Proc’s  
# are passed a modified version of the options hash that was  
# given to #to\_xml.  
#  
# proc = Proc.new { |options| options[:builder].tag!(‘abc’,  
‘def’) }  
# firm.to\_xml :procs =\> [proc]  
#  
#   
# # … normal attributes as shown above …  
# def  
#   
#  
# You may override the to\_xml method in your ActiveRecord::Base  
# subclasses if you need to. The general form of doing this is  
#  
# class IHaveMyOwnXML \< ActiveRecord::Base  
# def to\_xml(options = {})  
# options[:indent] ||= 2  
# xml = options[:builder] ||= Builder::XmlMarkup.new  
(:indent =\> options[:indent])  
# xml.instruct! unless options[:skip\_instruct]  
# xml.level\_one do  
# xml.tag!(:second\_level, ‘content’)  
# end  
# end  
# end

- Lourens

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [December 11, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/render-from-your-model/79049/5 "2006-12-11T22:11:51Z")

</div>

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](http://blog.yanime.org/articles/2006/08/05/rails-calling-render-outside-your-controllers)

Thanks for all your ideas and help!
