# Routing/format (I'm lost.)

**URL:** <https://www.ruby-forum.com/t/routing-format-im-lost/176525>\
**Category:** Rails\
**Created:** [October 1, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525 "2009-10-01T16:59:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 1, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/1 "2009-10-01T16:59:50Z")

</div>

I understand that the default Rails routing matches URLs with the  
form:

/controller/action/id.format

Default values exist for each of the parts to the right of the  
controller. I also understand that the default format is html. So,  
for example, the URL:

/blog/read/23

would route a request to the ‘read’ action of the ‘blog’ controller,  
set the :id param to ‘23’, and and render the html view ‘read.rhtml’

Now, if the URL were altered to:

/blog/read/23.csv

Apparently something different happens; but, I’m not at all clear on  
exactly what. Does this mean that I should setup a read.csv view to  
render the document in csv, or what?

I don’t seem to have enough basic knowledge to get started  
understanding this. I would really appreciate it if someone could  
help me get started off in the right direction. Thanks for any input.

```
      ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 1, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/2 "2009-10-01T20:35:17Z")

</div>

> Have you read this?  
> [Rails Routing from the Outside In — Ruby on Rails Guides](http://guides.rubyonrails.org/routing.html)

Yes, I have. However, because of your referral I have taken a fresh  
look. It is still none too clear to me. Here is what I get:

The respond\_to block allows us to assign separate action code and a  
separate response to each different format. I’m thinking that a  
“response” is a view template; but, I’m not sure. It also appears  
that part of what is going on is to get render to set the content-type  
header appropriately. For example, the ‘render :xml’ causes the  
content-type header to be set to “application/xml”. I’m not sure how  
the template file should be named. For example, should it be,  
“read.csv”? I don’t know what content-type is associated with the csv  
format.

As you can see, I still have quite a bit missing. Any further  
clarifying comments would be appreciated. Thanks.

```
     ... doug

```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 1, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/3 "2009-10-01T17:15:39Z")

</div>

On Thu, Oct 1, 2009 at 4:58 PM, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I understand that the default Rails routing matches URLs with the  
> form:
> 
> /controller/action/id.format

Have you read this?

> **[Rails Routing from the Outside In — Ruby on Rails Guides](https://guides.rubyonrails.org/routing.html)**
>
> Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred...

> ```
> ... doug
> 
> ```

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 1, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/4 "2009-10-01T21:33:15Z")

</div>

On 10/1/09, djolley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> that part of what is going on is to get render to set the content-type  
> header appropriately. For example, the ‘render :xml’ causes the  
> content-type header to be set to “application/xml”. I’m not sure how  
> the template file should be named. For example, should it be,  
> “read.csv”? I don’t know what content-type is associated with the csv  
> format.
> 
> As you can see, I still have quite a bit missing. Any further  
> clarifying comments would be appreciated. Thanks.
> 
> ```
> ... doug
> 
> ```

Ok, just to clarify a bit more, as Marnen said, a template is not  
mandatory, is just the default.  
Take a look a this example:  
[http://pastie.org/638500](http://pastie.org/638500)

I took it for some book and modified it just a bit.  
With format html, it will search for the default export.html.erb  
template, but for csv, it will behave differently.  
The example here uses fastercsv, which will let you handle csv in a  
nice way, but nevermind it for now, you should asume that you’ll have  
a variable with the csv data you want to return, and you’ll return it  
with the send\_data method (you can chech de ri doc for this later),  
setting the content-type and suggesting a file name to be saved.  
So my point here is to show you how you can answer from an action  
without having a template and considering more that one format.  
Anyway, it seems that you’re focusing on the wrong thing, you should  
read more about ActionController (ri doc or the guide on the ruby on  
rails site should be fine), this is not a routing problem.

Hope it helps.

Cheers.

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 1, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/5 "2009-10-01T20:41:04Z")

</div>

Doug J. wrote:

> > Have you read this?  
> > [Rails Routing from the Outside In — Ruby on Rails Guides](http://guides.rubyonrails.org/routing.html)
> 
> Yes, I have. However, because of your referral I have taken a fresh  
> look. It is still none too clear to me. Here is what I get:
> 
> The respond\_to block allows us to assign separate action code and a  
> separate response to each different format. I’m thinking that a  
> “response” is a view template; but, I’m not sure.

No. The response _is_ the respond\_to code block for the given format.  
If you don’t tell it otherwise, it will fnish by rendering a template of  
the same name as the action, but it doesn’t have to.

> It also appears  
> that part of what is going on is to get render to set the content-type  
> header appropriately. For example, the ‘render :xml’ causes the  
> content-type header to be set to “application/xml”. I’m not sure how  
> the template file should be named. For example, should it be,  
> “read.csv”? I don’t know what content-type is associated with the csv  
> format.

You can look up the MIME type for CSV in any list of standard MIME types  
(there are many available on the Web). As for naming the template, the  
pattern is action.format.renderer . So show.html.erb is called from the  
“show” action, and is processed by the ERb renderer to produce an HTML  
response.

> As you can see, I still have quite a bit missing. Any further  
> clarifying comments would be appreciated. Thanks.
> 
> ```
> ... doug
> 
> ```

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [October 2, 2009, 5:41am UTC](https://www.ruby-forum.com/t/routing-format-im-lost/176525/6 "2009-10-02T05:41:29Z")

</div>

> The example

If a picture is worth 1,000 words, an example is worth at least  
2,000. I easily got it working following your example. That doesn’t  
mean that I understand everything. I still have some learning to do.  
The thing is that at least now I feel like I have a significant toe  
hold to use in mastering this concept. Before, I was just plain  
totally lost. Thanks to all for the help.

```
     ... doug
```
