Forum: Ruby on Rails How to add format to RESTful route link_to helper

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Wes (Guest)
on 2007-02-26 18:13
(Received via mailing list)
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?

Trevor S. (Guest)
on 2007-02-26 18:44
(Received via mailing list)

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

Tom L. (Guest)
on 2007-02-27 04:18
(Received via mailing list)
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 L.
Wes (Guest)
on 2007-02-27 21:03
(Received via mailing list)
Thanks Trevor and Tom!
Wes (Guest)
on 2007-02-28 02:15
(Received via mailing list)
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!
This topic is locked and can not be replied to.