Hi,
I have an array with objects and I want to generate an XML like:
<objects>
<object>
<category_id>1</category_id>
<helper-method>result 1</helper-method>
</object>
<object>
<category_id>2</category_id>
<helper-method>result 2</helper-method>
</object>
</objects>
The helper method generates some urls and it needs as an arguments the
object.
What would be a proper way to include the result of that method call?
I guess building the xml manually could work, but that means I would
have to manually enter every object variable.
--
M.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
modify the to_xml method of your object, like so:
class MyObject < ActiveRecord::Base
def helper_method(obj)
"<helper-method>something-from-obj-#{obj.id}</helper-method>"
end
def to_xml
xml = super
close_tag = "</#{self.class.to_s.underscore.dasherize}>"
xml.gsub(close_tag, " #{self.helper_method(self)}\n#{close_tag}")
end
end
On Nov 10, 10:15 am, Marcelo Barbudas
<nos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi,
>
> I have an array with objects and I want to generate an XML like:
>
> <objects>
> <object>
> <category_id>1</category_id>
> <helper-method>result 1</helper-method>
> </object>
> <object>
> <category_id>2</category_id>
> <helper-method>result 2</helper-method>
> </object>
> </objects>
>
> The helper method generates some urls and it needs as an arguments the
> object.
>
> What would be a proper way to include the result of that method call?
>
> I guess building the xml manually could work, but that means I would
> have to manually enter every object variable.
>
> --
> M.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
My problem is that the helper method, is a Helper(app/helpers) Method. :) And I can''t use it in the model. Otherwise I could have called to_xml ( :method => etc). -- M. On Nov 11, 9:50 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> modify theto_xmlmethod of your object, like so: > > class MyObject < ActiveRecord::Base > def helper_method(obj) > "<helper-method>something-from-obj-#{obj.id}</helper-method>" > end > > defto_xml > xml = super > close_tag = "</#{self.class.to_s.underscore.dasherize}>" > xml.gsub(close_tag, " #{self.helper_method(self)}\n#{close_tag}") > end > end > > On Nov 10, 10:15 am, Marcelo Barbudas <nos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I have an array with objects and I want to generate an XML like: > > > <objects> > > <object> > > <category_id>1</category_id> > > <helper-method>result 1</helper-method> > > </object> > > <object> > > <category_id>2</category_id> > > <helper-method>result 2</helper-method> > > </object> > > </objects> > > > The helper method generates some urls and it needs as an arguments the > > object. > > > What would be a proper way to include the result of that method call? > > > I guess building the xml manually could work, but that means I would > > have to manually enter every object variable. > > > -- > > M.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In your model, you should just be able to include the helper: class MyObject < ActiveRecord::Base include SomeHelper end On Nov 12, 9:02 am, Marcelo Barbudas <nos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My problem is that the helper method, is a Helper(app/helpers) > Method. :) > > And I can''t use it in the model. Otherwise I could have called to_xml > ( :method => etc). > > -- > M. > > On Nov 11, 9:50 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > modify theto_xmlmethod of your object, like so: > > > class MyObject < ActiveRecord::Base > > def helper_method(obj) > > "<helper-method>something-from-obj-#{obj.id}</helper-method>" > > end > > > defto_xml > > xml = super > > close_tag = "</#{self.class.to_s.underscore.dasherize}>" > > xml.gsub(close_tag, " #{self.helper_method(self)}\n#{close_tag}") > > end > > end > > > On Nov 10, 10:15 am, Marcelo Barbudas <nos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > > I have an array with objects and I want to generate an XML like: > > > > <objects> > > > <object> > > > <category_id>1</category_id> > > > <helper-method>result 1</helper-method> > > > </object> > > > <object> > > > <category_id>2</category_id> > > > <helper-method>result 2</helper-method> > > > </object> > > > </objects> > > > > The helper method generates some urls and it needs as an arguments the > > > object. > > > > What would be a proper way to include the result of that method call? > > > > I guess building the xml manually could work, but that means I would > > > have to manually enter every object variable. > > > > -- > > > M.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---