Downloadable XML Files, Named Routes and RESTful ability

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.

"
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 or a 9.xml will generate a html file
or an xml file based on the RESTful format.

“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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs