class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
...
This is from the Ruby on Rails tutorial: http://edgeguides.rubyonrails.org/getting_started.html#setting-the-application-home-page
respond_to
is a rails specific method that defines how requests for different formats (like xml and html) are responded to. The do
and |format|
delineate a ruby block, with do
acting like a open brace and end
as a closing brace, and |format|
defines the block variable that gets its value from the yield
statement within responds_to
.
the "do" is a RUBY block, and the "|format|" could be anything, its just a variable to use inside that block, here is another example:
respond_to do |x|
x.html # index.html.erb
x.xml { render :xml => @posts }
end
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