Datasheet
Chapter 1: Building Resources
11
traditional rails route — parts of the prefix specified as a Ruby symbol are converted to variables when
the path is dereferenced. For example, if you wanted all recipes to be attached to a chef, you could add the
option
:path_prefix = > “ /chef/:chef_name ” , and the show recipe URL, for example, would change
to
/chef/juliachild/recipe/1 . Within the controller, the variable params[:chef_name] would be
set to
juliachild .
Controllers
The controller for each new resource contains seven actions, shown earlier in the table of standard
routes. Each action is helpfully commented with the URLs that cause that action to be invoked. Each
action is also set up by default to respond to both HTML and XML requests. Following are sections about
the default controllers for the recipe resource with some comments.
Index
First up, the index method, which displays a list of all the recipes:
# GET /recipes
# GET /recipes.xml
def index
@recipes = Recipe.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml = > @recipes }
end
end
If you ’ re familiar with traditional Rails, than the only new part here is the respond_to method, which is
the REST mechanism that allows the same controller action to return different data based on the
requested format.
Functionally what happens here is similar to a case expression — each potential format that the action
might respond to is listed in the body of the
respond_to block, and exactly one of them is performed
based on the MIME type of the user request. In this case, if the URL request is
/recipes or /recipes.
html
, then the format.html line is chosen. If the URL is /recipes.xml , then the format.xml line is
chosen. Each type can have a block associated with it, which is executed when that type matches the user
request. If there is no block associated with the type, then the Rails default action for dealing with that
type is triggered. In the case of the
html action, that would be the rendering of the matching html.erb
view,
index.html.erb . It has become customary to explicitly note that the format is being handled in a
default manner with a comment naming the view file to be rendered.
Since this is one of those Ruby metaprogramming magic things, where it ’ s not immediately clear what ’ s
happening behind the scenes, it ’ s worth breaking the method down a little bit. The
respond_to method
comes in two forms. The one shown previously takes a block. Alternately, you could just pass a list of
symbols corresponding to types
(:html, :js) . You would use the list version if every type on the list
was handled via the default action for that type.
In the more typical case, the block is defined with a single argument. The argument is of a
Responder
class. Each line of the block calls a method on the responder object — in the previous code, those
methods are
format.html and format.xml . Each of these format methods takes an optional argument,
which is also a block.
c01.indd 11c01.indd 11 1/30/08 4:02:24 PM1/30/08 4:02:24 PM