# How to add format to RESTful route link\_to helper

**URL:** <https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873>\
**Category:** Rails\
**Created:** [February 26, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873 "2007-02-26T17:13:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@wes](https://www.ruby-forum.com/u/wes)\
**Post date:** [February 26, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873/1 "2007-02-26T17:13:18Z")

</div>

Question: How do I tack a format (.xls) onto a REST url helper method  
in link\_to?

I have a route created from map.resources in my routes.rb file called  
“comments”.

map.resources :posts do |post|  
post.resources :comments  
end

How do I use the auto-generated REST url helper methods in my link\_to  
to specify that I want all the comments for a particular post in XLS  
format?

In my view:

# This shows all comments in (default) HTML - I want it to append .xls

to the URL  
\<%= link\_to “Export to Excel”, comments\_url(@post) %\>

How would I tell it to append .xls onto the generated link so that I  
can catch this in my controller and respond with an Excel file?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![wes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@wes](https://www.ruby-forum.com/u/wes)\
**Post date:** [February 26, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873/2 "2007-02-26T17:44:22Z")

</div>

Hey

link\_to “Export to Excel”, formatted\_comments\_url(@post, :xls)

Trevor

---

<div class="post-metadata">

**Author:** ![wes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@wes](https://www.ruby-forum.com/u/wes)\
**Post date:** [February 27, 2007, 3:18am UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873/3 "2007-02-27T03:18:46Z")

</div>

And for your future REST-related questions, the “RESTful Rails” cheat  
sheet is extremely handy:

[http://peepcode.com/products/restful-rails](http://peepcode.com/products/restful-rails)

(look for the “Download Free REST Cheat Sheet” link)

Sincerely,

Tom L.  
[http://AllTom.com/](http://AllTom.com/)  
[http://GadgetLife.org/](http://GadgetLife.org/)

---

<div class="post-metadata">

**Author:** ![wes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@wes](https://www.ruby-forum.com/u/wes)\
**Post date:** [February 28, 2007, 1:15am UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873/4 "2007-02-28T01:15:56Z")

</div>

One last question - is there a way to DRY this up a bit? The :template  
parameter of the :xls format especially.

I’m using an XML builder to build an XML-based Excel file -  
[http://wiki.rubyonrails.org/rails/pages/HowToExportToExcel](http://wiki.rubyonrails.org/rails/pages/HowToExportToExcel).

def index  
@comments = Comment.find\_all\_by\_post\_id(@post)  
respond\_to do |format|  
format.html  
format.xml { render :xml =\> @commets.to\_xml }  
format.xls { render\_without\_layout :xml =\> @comments, :template  
=\> “comments/index.rxml” }  
end  
end

Thanks again guys!  
Wes

---

<div class="post-metadata">

**Author:** ![wes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@wes](https://www.ruby-forum.com/u/wes)\
**Post date:** [February 27, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/how-to-add-format-to-restful-route-link-to-helper/86873/5 "2007-02-27T20:03:15Z")

</div>

Thanks Trevor and Tom!
