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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, Why not just use to_xml() ? ( Rails 1.2 & Edge ) See http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000934 and http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/ Hash/Conversions.html - Lourens On 2006/12/11, at 20:46, Mike Evron wrote:> > 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. > > -- > Posted via http://www.ruby-forum.com/. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ]
#
# <firm>
# # ... normal attributes as shown above ...
#
<calculated-earnings>100000000000000000</calculated-earnings>
# <real-earnings>5</real-earnings>
# </firm>
#
# 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 ]
#
# <firm>
# # ... normal attributes as shown above ...
# <abc>def</abc>
# </firm>
#
# 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
http://blog.methodmissing.com
On 2006/12/11, at 21:10, Wiliam Langshaw wrote:
>
> 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!
>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---