Possible Duplicates:
What is the best way to learn Ruby?
Explain Iterator Syntax on Ruby on Rails
I'm still learning ruby, ruby on rails and such. I'm getting better at understanding all the ruby and rails syntax but this one has me a little stumped.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @contact_lists }
end
respond_to is a method that takes a proceedure, I think. The two formats look like they may be method calls too, but I don't know.
respond_to
is a method which takes block. The block takes one argument, which here is called format
.
Now you call two methods on format
. html
which you call without arguments. And xml
which you call with a block.
This block takes no arguments and contains a call to the render method with a hash as an argument. The hash contains the key :xml
and the value @contact_lists
.
Yeap, you're right.
Ruby method calls are a bit puzzling at first, because you can ommit the parethesis, and they may receive code blocks.
So, this is the explaination:
respond_to do |format|
Invoke the method respond_to
and pass it a block on what to do with the format
it will receive.
format.html # index.html.erb
With that object called format
invoke the method html
format.xml { render :xml => @contact_lists }
And the method xml
which in turns receive another block ( do / en and { } , are different syntax to pass block. )
end
Finish the first block
See this other , other answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With