On 14-01-03, 14:06, Warren Zhang wrote:
> I don't see how the code "tells" the format object to perform the
> default action or XML action.

The answer is a bit more about rails than ruby itself, but:

The format (Responder object) has methods that correspond to registered 
mime types (xml, html, json, or you can add your own). None of these is 
considered a "default" but the request.format is determined from either:

* Accepts header (parsed by Rack I believe)
* route-specific :format option in config/routes.rb
   (e.g. traditionally /:controller/:action/:id.:format )

What those methods do is:

* If no block is passed (like format.html) then configure the
   renderer to process the template file that matches the name of the
   action, e.g. /views/posts/show.html.erb
* If a block is passed, then configure the renderer to call the block
   for the response body.

The actual render call doesn't happen until later. You could call it 
manually in your method (render "posts/show"), but when you don't then 
the controller will see that it wasn't called, and call the appropriate 
responder method instead.

So, it's a bit of rails magic, and nothing like a case statement.