How to target format with #{controller}_path URL helper in R

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

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

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?

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

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
)