glenn
2007-Feb-20 12:23 UTC
How to target format with #{controller}_path URL helper in REST?
I''m migrating my site over to a more RESTful style and I''m having some trouble with the URL helpers. If I have two links on my page, how to I make one target the .html format of a given resource and the other to target the .xml format? Thanks, Glenn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter De Berdt
2007-Feb-20 12:31 UTC
Re: How to target format with #{controller}_path URL helper in REST?
On 20 Feb 2007, at 13:23, glenn wrote:> I''m migrating my site over to a more RESTful style and I''m having some > trouble with the URL helpers. If I have two links on my page, how to I > make one target the .html format of a given resource and the other to > target the .xml format?REST is all about using the HTTP protocol to determine which format to serve. I''m assuming you want to use RESTful routes. If the HTTP header asks for an XML representation, your method will return XML, if (X)HTML is requested, it will serve a normal web page. Generate a RESTful scaffold using "script/generate scaffold_resource something field1:string field2:string" and have a look at the code. There are ways around this, but it''s better to stick with the convention. For urls with a certain format, you can use the "formatted_url" helper. Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
glenn
2007-Feb-20 13:45 UTC
Re: How to target format with #{controller}_path URL helper in REST?
> REST is all about using the HTTP protocol to determine which format > to serve. I''m assuming you want to use RESTful routes. If the HTTP > header asks for an XML representation, your method will return XML, > if (X)HTML is requested, it will serve a normal web page. Generate a > RESTful scaffold using "script/generate scaffold_resource something > field1:string field2:string" and have a look at the code. > There are ways around this, but it''s better to stick with the > convention. For urls with a certain format, you can use the > "formatted_url" helper.Peter, Let me specify another example that might help clarify things. Say I had my site generating a report of some format, and I''d like the data to to be offered in a range of formats such as (x)html, xml, and csv. All of these are currently accounted for with the standard map.resources routes and a respond_to block. What I want to know is how I can display a link to the xml or csv version when the viewer is viewing the x(html) version? We all know that /reports/1.xml will get what I''m asking for, but the url_for and other various helpers don''t appear to take a :format value and I''m loathe to just semi-hard code in the output I need incase routes change in the future. If I can keep using the URL helpers it''s more likely to not cause issues later. Any ideas? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jean-François Trân
2007-Feb-20 14:02 UTC
Re: How to target format with #{controller}_path URL helper in REST?
Glenn,> Peter : > > There are ways around this, but it''s better to stick with the > > convention. For urls with a certain format, you can use the > > "formatted_url" helper. > > Peter, > > Let me specify another example that might help clarify things. > Say I had my site generating a report of some format, and I''d > like the data to to be offered in a range of formats such as > (x)html, xml, and csv. > All of these are currently accounted for with the standard > map.resources routes and a respond_to block. What I want > to know is how I can display a link to the xml or csv version > when the viewer is viewing the x(html) version? We all know > that /reports/1.xml will get what I''m asking for, but the url_for > and other various helpers don''t appear to take a :format value > and I''m loathe to just semi-hard code in the output I need incase > routes change in the future. If I can keep using the URL > helpers it''s more likely to not cause issues later.I''m afraid you haven''t read Peter''s end of answer carefully. He said "For urls with a certain format, you can use the "formatted_url" helper." that is, for example : <%= formatted_report_path(1, :xml) %> or <%= formatted_report_url(1, :xml) %> -- Jean-François. -- Ruby ( http://www.rubyfrance.org ) on Rails ( http://www.railsfrance.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
glenn
2007-Feb-20 14:05 UTC
Re: How to target format with #{controller}_path URL helper in REST?
> I''m afraid you haven''t read Peter''s end of answer carefully. > He said "For urls with a certainformat, you can use the > "formatted_url" helper." > > that is, for example : > > <%= formatted_report_path(1, :xml) %>Jean-François & Peter, Exactly what I was after! Apologies for that, couldn''t seem to find any mention of it in the API docs. Thanks, Glenn --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---