J
Joshua Beall
Hi All,
I'm new to both Ruby and Rails (I come from an ASP.NET/C# background,
mainly, and I also have done a fair amount of PHP). I'm looking at
these lines from my controller:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
And I just can't quite get my head around it completely. I know the end
result is that it renders different types of output. But I don't
understand how it's executing. I read the docs here:
http://www.cs.auckland.ac.nz/references/ruby/doc_bundle/Manual/man-1.4/syntax.html
Which states:
" method_call do [`|' expr...`|'] expr...end
The method may be invoked with the block (do .. end or {..}). The method
may be evaluate back that block from inside of the invocation. The
methods that calls back the blocks are sometimes called as iterators.
The evaluation of the block from iterator is done by yield. "
But I still don't understand where |format| is coming from, or what it's
doing. From what I've seen in Ruby, I think of something like |format|
as being part of a foreach type block. As in "possible_formats.each do
|format|".
So this is what I'm seeing in my head when I look at that block:
for(format in ????)
respond_to(format.html) # index.html.erb will be rendered by default
respond_to(format.xml , { render :xml => @posts })
end
But that can't be right, because there's nothing for ???? to be resolved
to...
Can someone help clear this up for me?
I'm new to both Ruby and Rails (I come from an ASP.NET/C# background,
mainly, and I also have done a fair amount of PHP). I'm looking at
these lines from my controller:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
And I just can't quite get my head around it completely. I know the end
result is that it renders different types of output. But I don't
understand how it's executing. I read the docs here:
http://www.cs.auckland.ac.nz/references/ruby/doc_bundle/Manual/man-1.4/syntax.html
Which states:
" method_call do [`|' expr...`|'] expr...end
The method may be invoked with the block (do .. end or {..}). The method
may be evaluate back that block from inside of the invocation. The
methods that calls back the blocks are sometimes called as iterators.
The evaluation of the block from iterator is done by yield. "
But I still don't understand where |format| is coming from, or what it's
doing. From what I've seen in Ruby, I think of something like |format|
as being part of a foreach type block. As in "possible_formats.each do
|format|".
So this is what I'm seeing in my head when I look at that block:
for(format in ????)
respond_to(format.html) # index.html.erb will be rendered by default
respond_to(format.xml , { render :xml => @posts })
end
But that can't be right, because there's nothing for ???? to be resolved
to...
Can someone help clear this up for me?