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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
813679171280808f051b5754c9db45a6?d=identicon&s=25 Wes (Guest)
on 2007-02-26 17: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
"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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2007-02-26 17:44
(Received via mailing list)
Hey

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

Trevor
7a28bf7b0932a9486c21f1a33eb431ba?d=identicon&s=25 Tom Lieber (Guest)
on 2007-02-27 03:18
(Received via mailing list)
And for your future REST-related questions, the "RESTful Rails" cheat
sheet is extremely handy:

http://peepcode.com/products/restful-rails

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

Sincerely,

Tom Lieber
http://AllTom.com/
http://GadgetLife.org/
813679171280808f051b5754c9db45a6?d=identicon&s=25 Wes (Guest)
on 2007-02-27 20:03
(Received via mailing list)
Thanks Trevor and Tom!
813679171280808f051b5754c9db45a6?d=identicon&s=25 Wes (Guest)
on 2007-02-28 01: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 -
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
This topic is locked and can not be replied to.