# RESTful Linking in to\_xml()

**URL:** <https://www.ruby-forum.com/t/restful-linking-in-to-xml/113201>\
**Category:** Rails\
**Created:** [August 28, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/restful-linking-in-to-xml/113201 "2007-08-28T17:51:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geoffrey\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/57b2e6/32.png) [@Geoffrey\_W](https://www.ruby-forum.com/u/Geoffrey_W)\
**Post date:** [August 28, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/restful-linking-in-to-xml/113201/1 "2007-08-28T17:51:41Z")

</div>

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](http://myserver/customers/1.xml)  
  
  
2  
Pepsi  
[http://myserver/customers/2.xml](http://myserver/customers/2.xml)

Alternately, it could be as simple as:  
  
[Coke](http://myserver/customers/1.xml)  
[Pepsi](http://myserver/customers/2.xml)

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. 😉 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

---

<div class="post-metadata">

**Author:** ![Geoffrey\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/57b2e6/32.png) [@Geoffrey\_W](https://www.ruby-forum.com/u/Geoffrey_W)\
**Post date:** [August 28, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/restful-linking-in-to-xml/113201/2 "2007-08-28T21:09:46Z")

</div>

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. 😉 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

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.
