Forum: Ruby on Rails How to target format with #{controller}_path URL helper in R

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.
glenn (Guest)
on 2007-02-20 14:24
(Received via mailing list)
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
Peter De Berdt (Guest)
on 2007-02-20 14:32
(Received via mailing list)
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
glenn (Guest)
on 2007-02-20 15:46
(Received via mailing list)
> 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?
Jean-François Trân (Guest)
on 2007-02-20 16:02
(Received via mailing list)
Glenn,
> (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
)
glenn (Guest)
on 2007-02-20 16:06
(Received via mailing list)
> 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
This topic is locked and can not be replied to.