I’m going through the Getting Started with Rails guide and got confused with section 6.7. After generating a scaffold I find the following auto-generated block in my controller:
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @posts }
end
end
I’d like to understand how the respond_to block actually works. What type of variable is format? Are .html and .json methods of the format object? The documentation for
ActionController::MimeResponds::ClassMethods::respond_to
doesn’t answer the question.
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.dountilend, with|format|as an argument to the block.formatargument.http://api.rubyonrails.org/v4.1/classes/ActionController/Responder.html
Responderdoes NOT contain a method for.htmlor.json, but we call these methods anyways! This part threw me for a loop.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
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):
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 leveragemethod_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_towas extracted into responders gem.