Routing/format (I'm lost.)

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

Have you read this?
Rails Routing from the Outside In — Ruby on Rails Guides

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

On Thu, Oct 1, 2009 at 4:58 PM, doug [email protected] wrote:

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

/controller/action/id.format

Have you read this?

     ... doug


Leonardo M…
There’s no place like ~

On 10/1/09, djolley [email protected] 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

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 ~

Doug J. wrote:

Have you read this?
Rails Routing from the Outside In — Ruby on Rails Guides

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
[email protected]

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