How to add format to RESTful route link_to helper

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

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

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

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?



link_to "Export to Excel", formatted_comments_url(@post, :xls)


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

(look for the "Download Free REST Cheat Sheet" link)


Tom Lieber

Thanks Trevor and Tom!

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 -

  def index
    @comments = Comment.find_all_by_post_id(@post)
    respond_to do |format|
      format.xml { render :xml => @commets.to_xml }
      format.xls { render_without_layout :xml => @comments, :template
=> "comments/index.rxml" }

Thanks again guys!