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

**URL:** <https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180>\
**Category:** Rails\
**Created:** [February 20, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180 "2007-02-20T13:24:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [February 20, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180/1 "2007-02-20T13:24:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [February 20, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180/2 "2007-02-20T13:32:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [February 20, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180/3 "2007-02-20T14:46:37Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [February 20, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180/4 "2007-02-20T15:06:40Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [February 20, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-target-format-with-controller-path-url-helper-in-r/86180/5 "2007-02-20T15:02:39Z")

</div>

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](http://www.rubyfrance.org) ) on Rails ( [http://www.railsfrance.org](http://www.railsfrance.org)  
)
