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