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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2007-Feb-26 16:42 UTC
Re: How to add format to RESTful route link_to helper
Hey link_to "Export to Excel", formatted_comments_url(@post, :xls) Trevor On 26-Feb-07, at 8:12 AM, Wes wrote:> > 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Trevor and Tom! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---