RESTful Linking in to_xml()

If I have a resource list, I’d ideally like that resource list to link
to the resources. So, for example, if I have a list of Customers that
can be retrieved via GET /customers.xml, I’d like it to come out
something like this:


1
Coke
http://myserver/customers/1.xml


2
Pepsi
http://myserver/customers/2.xml

Alternately, it could be as simple as:

Coke
Pepsi

This kind of linking helps a client use the API without having to have
its own knowledge of one’s routes.

However, Rails seems to be getting in my way of getting this
done. :wink: I can call:
render :xml => @customers.to_xml( :only=>[:id,:name] )

This gets me everything but the URL. To get the URL, I looked
at :procs and :methods, but both seem to be invoked on the controller,
which has access to customer_path or url_for, but not to the ID of the
particular customer of which my collection has iterated.

Suggestions? I’d like to make this happen, ideally using one of the
URL helpers (which save the model from having to know anything about
the route), but I can’t find a part of the XML process that is aware
of the helpers and the customer id; it seems like you’d want the proc/
method to have access to the object being XML’d, but as far as I can
see, it doesn’t have access to that.

  • Geoffrey

Geoffrey W. wrote:

If I have a resource list, I’d ideally like that resource list to link
to the resources. So, for example, if I have a list of Customers that
can be retrieved via GET /customers.xml, I’d like it to come out
something like this:

[snip]

This kind of linking helps a client use the API without having to have
its own knowledge of one’s routes.

However, Rails seems to be getting in my way of getting this
done. :wink: I can call:
render :xml => @customers.to_xml( :only=>[:id,:name] )

This gets me everything but the URL. To get the URL, I looked
at :procs and :methods, but both seem to be invoked on the controller,
which has access to customer_path or url_for, but not to the ID of the
particular customer of which my collection has iterated.

Suggestions? I’d like to make this happen, ideally using one of the
URL helpers (which save the model from having to know anything about
the route), but I can’t find a part of the XML process that is aware
of the helpers and the customer id; it seems like you’d want the proc/
method to have access to the object being XML’d, but as far as I can
see, it doesn’t have access to that.

In my experience the options that to_xml will take will only suffice for
the simplest of cases: including or excluding model attributes and
including the default output of first-order associations.

Anything more or custom than that, and you’ll want to override to_xml or
write a custom XML builder. That’s what I’d recommend doing here as
well. If you override to_xml then you will still suffer from not having
the URL helpers available in the model, which is why creating a custom
XML builder (i.e. format.xml do … end or rendering a view named
index.xml.builder) may serve you better.


Roderick van Domburg