Wes
(Wes)
1
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
Hey
link_to "Export to Excel", formatted_comments_url(@post, :xls)
Trevor
Tom_Lieber
(Tom Lieber)
3
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/
Wes
(Wes)
5
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