I am new to Ruby and got stuck at this same code. The parts that I got hung up on were a little more fundamental than some of the answers I found here. This may or may not help someone.
respond_tois a method on the superclassActionController.- it takes a block, which is like a delegate. The block is from
dountilend, with|format|as an argument to the block. - respond_to executes your block, passing a Responder into the
formatargument.
http://api.rubyonrails.org/v4.1/classes/ActionController/Responder.html
- The
Responderdoes NOT contain a method for.htmlor.json, but we call these methods anyways! This part threw me for a loop. - Ruby has a feature called
method_missing. If you call a method that doesn’t exist (likejsonorhtml), Ruby calls themethod_missingmethod instead.
http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html
- The
Responderclass uses itsmethod_missingas a kind of registration. When we call ‘json’, we are telling it to respond to requests with the .json extension by serializing to json. We need to callhtmlwith no arguments to tell it to handle .html requests in the default way (using conventions and views).
It could be written like this (using JS-like pseudocode):
// get an instance to a responder from the base class
var format = get_responder()
// register html to render in the default way
// (by way of the views and conventions)
format.register('html')
// register json as well. the argument to .json is the second
// argument to method_missing ('json' is the first), which contains
// optional ways to configure the response. In this case, serialize as json.
format.register('json', renderOptions)
This part confused the heck out of me. I still find it unintuitive. Ruby seems to use this technique quite a bit. The entire class (responder) becomes the method implementation. In order to leverage method_missing, we need an instance of the class, so we’re obliged to pass a callback into which they pass the method-like object. For someone who has coded in C-like languages for 20 some years, this is very backwards and unintuitive to me. Not that it’s bad! But it’s something a lot of people with that kind of background need to get their head around, and I think might be what the OP was after.
p.s. note that in RoR 4.2 respond_to was extracted into responders gem.