Forum: Ruby on Rails .to_xml on an index, custom set response by Object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
39c54090f64dbb313b97e007aec711e3?d=identicon&s=25 MattFS218 (Guest)
on 2009-02-26 10:44
(Received via mailing list)
I have a Contacts controller, that I'm using to output contacts when
an index is called, but the XML that I will output for each contact
varies. I'm not having any success using :proc or :method

:method could work, but the string my method returns, escapes the <>
characters I'm using trying to "fake" XML mark-up.

Basically, each contact has many Kvs, and I only want to :include
output some KV (not all) for each contact. I can :include kvs but that
will include ALL. I cannot used proc, because there's no way for proc
to "know" which individual contact it's being run for.

Can anyone recommend an easy way around this?

--matt
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-26 10:46
(Received via mailing list)
On Feb 26, 9:28 am, MattFS218 <mattdf...@gmail.com> wrote:
> to "know" which individual contact it's being run for.
It seems like the easiest way would be a method that calls to_xml on
those kvs you want included. Not sure what you meant about fake xml
mark-up in your comment above. Another way might be to :include an
association whose conditions restrict it to those Kvs you are
interested in.

Fred

Fred
39c54090f64dbb313b97e007aec711e3?d=identicon&s=25 MattFS218 (Guest)
on 2009-02-26 11:09
(Received via mailing list)
Hey Fred,

Thanks for the rapid response.

But when I

format.fxml  { render :fxml => @contacts.to_fxml(:methods=>
[:my_action] ) }

and

def my_action()
  return self.kvs.to_xml
end

the resulting XML has the < and > symbols escaped out &lt;?xml
version=&quot;1.0&quot;

Unless I'm mistaken, but associations are per Object, correct? Not per
instance. the Kvs i'm trying to output vary per instance. Any other
recommendations?

--matt

On Feb 26, 4:45 pm, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-26 11:17
(Received via mailing list)
On Feb 26, 10:08 am, MattFS218 <mattdf...@gmail.com> wrote:
>
> def my_action()
>   return self.kvs.to_xml
> end
>
> the resulting XML has the < and > symbols escaped out &lt;?xml
> version=&quot;1.0&quot;
>
Oh I get you.

> Unless I'm mistaken, but associations are per Object, correct? Not per
> instance. the Kvs i'm trying to output vary per instance. Any other
> recommendations?
You can have interpolated conditions on an association (but this does
rely on you being able to express your conditions in sql, prevents
eager loading etc...). Another way is a custom to_xml method on your
model, you'll get a Builder::XmlMarkup object given to you on which
you can generate any xml you want

Fred
39c54090f64dbb313b97e007aec711e3?d=identicon&s=25 MattFS218 (Guest)
on 2009-02-26 11:39
(Received via mailing list)
is it possible to get a quick example of how to override .to_xml?, and
maybe add a little bit of markup?

On Feb 26, 5:16 pm, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-26 12:01
(Received via mailing list)
On 26 Feb 2009, at 10:37, MattFS218 wrote:

>
> is it possible to get a quick example of how to override .to_xml?, and
> maybe add a little bit of markup?
>
THere's an example in the documentation for to_xml.

Fred
39c54090f64dbb313b97e007aec711e3?d=identicon&s=25 MattFS218 (Guest)
on 2009-02-26 12:06
(Received via mailing list)
The doc's give an example of entirely overwriting the output. All I
really wanna do is append another tag. I've tried using various
combinations of super, but super is returning a string. Is it possible
then, at least to get an example of how to override xml using super,
and appending a tag? I've been working on this for the past 3 hrs. Any
help would be greatly appreciated.

--matt

On Feb 26, 6:00 pm, Frederick Cheung <frederick.che...@gmail.com>
This topic is locked and can not be replied to.