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: <customers> <customer> <id>1</id> <name>Coke</name> <url>http://myserver/customers/1.xml</url> </customer> <customer> <id>2</id> <name>Pepsi</name> <url>http://myserver/customers/2.xml</url> </customer> </customers>
Alternately, it could be as simple as: <customers> <a href="http://myserver/customers/1.xml">Coke</a> <a href="http://myserver/customers/2.xml">Pepsi</a> </customers>
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