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_to
is a method on the superclass ActionController
.
- it takes a block, which is like a delegate. The block is from
do
until end
, with |format|
as an argument to the block.
- respond_to executes your block, passing a Responder into the
format
argument.
http://api.rubyonrails.org/v4.1/classes/ActionController/Responder.html
- The
Responder
does NOT contain a method for .html
or .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 (like json
or html
), Ruby calls the method_missing
method instead.
http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html
- The
Responder
class uses its method_missing
as 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 call html
with 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…