# Downloadable XML Files, Named Routes and RESTful ability

**URL:** <https://www.ruby-forum.com/t/downloadable-xml-files-named-routes-and-restful-ability/135553>\
**Category:** Rails\
**Created:** [April 23, 2008, 2:12am UTC](https://www.ruby-forum.com/t/downloadable-xml-files-named-routes-and-restful-ability/135553 "2008-04-23T02:12:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgtorre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rgtorre](https://www.ruby-forum.com/u/rgtorre)\
**Post date:** [April 23, 2008, 2:12am UTC](https://www.ruby-forum.com/t/downloadable-xml-files-named-routes-and-restful-ability/135553/1 "2008-04-23T02:12:46Z")

</div>

I have named routes like these:  
GET /products  
{:controller=\>“products”, :action=\>“index”}  
GET /products.:format  
{:controller=\>“products”, :action=\>“index”}  
product GET /products/:id  
{:controller=\>“products”, :action=\>“show”}  
formatted\_product GET /products/:id.:format  
{:controller=\>“products”, :action=\>“show”}

The GET /products and GET /products/:id would render the html listing  
of all products and and a specific product id, respectively. I wanted  
to place a button link like ‘Send XML Data’ in the views whenever I  
display the listing of products or showing just a page for a specific  
product id.

I created a ‘send\_xml\_data’ action/method in the Products controller  
with the intent of giving the user a choice to dowload the xml file of  
listing of products or a specifc product using this, thinking that  
this would prompt for a download (in this case the whole product  
list):

def send\_xml\_data  
@products = Product.find(:all)  
products\_file \<\< xml.product\_list(:for\_product =\>  
@products.title) do  
xml.description(@products.description)  
xml.image\_url(@products.image\_url)  
xml.price(@products.price)  
xml.created\_at(@products.created\_at)  
xml.updated\_at(@products.updated\_at)  
end  
send\_data products\_file, :filename =\> ‘product\_list.xml’  
end

When I created a button link to direct to the products controller and  
the action/method send\_xml\_data, I get this error from the SHOW action  
(GET /products/:id  
{:controller=\>“products”, :action=\>“show”})  
saying that “send\_xml\_data” is not a valid product id, even if I  
enabled in the config\routes.rb the "map.connect ‘:controller/:action’  
route with high priority . I always get routed to the show action GET  
product id.

It seems like the solution to send an xml file in the controller  
defining the following method below will not work.

1. 

"  
def get\_file  
my\_file = \<\<-FILE  
  
FILE  
send\_data my\_file, :filename =\> ‘myfile.xml’  
end  
"

Also, the method below will not work beacuse the xml template for a  
single product will be dynamic based on the respond\_to format. A URL  
[http://localhost:3000/products/9](http://localhost:3000/products/9) or a 9.xml will generate a html file  
or an xml file based on the RESTful format.

1. 

“send\_data render :template=\>”/route/gpx.rxml", :filename =\> “tb.xml”  
"

Is there a way I can code the routing link to from a show product id  
template or a listing template to give the user the ability to  
download an xml file while at the same time take advantage of named  
routes and the existing RESTful ability of the views that already  
render xml on the browser?

Thanks,  
Renato

---

<div class="post-metadata">

**Author:** ![rgtorre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rgtorre](https://www.ruby-forum.com/u/rgtorre)\
**Post date:** [April 24, 2008, 2:43am UTC](https://www.ruby-forum.com/t/downloadable-xml-files-named-routes-and-restful-ability/135553/2 "2008-04-24T02:43:25Z")

</div>

Found a solution one day after:

in Products controller:  
def send\_xml\_list  
@products = Product.find(:all)  
@xml\_products = @products.to\_xml  
send\_data( @xml\_products, :type =\> “text/xml”, :filename =\>  
“products.xml” )  
end

def show\_to\_xml  
@product = Product.find(params[:id])  
@xml\_product = @product.to\_xml  
send\_data( @xml\_product, :type =\> “text/xml”, :filename =\>  
“#{@product.id}.xml” )  
end

in index view  
\<%= button\_to ‘XML’, :action =\> ‘send\_xml\_list’%\>

in show view  
\<%= button\_to ‘XML’,:action =\> ‘show\_to\_xml’, :id =\> @product %\>

Not very DRY but I will refactor in one method that will will create  
the xml file(s) based on whether a product id param is passed or not.  
Hopefully, the results of the calls to Product.find method have been  
previously cached.

Renato
