Datasheet

Chapter 1: Building Resources
20
The redirection method automatically handles the list of nested resource objects. The destroy action
redirects to the list action, so you need to change that redirection as follows:
redirect_to(recipe_ingredients_url)
In this case, the controller automatically infers that it should use the @recipe attribute to generate the
correct index path.
The Views
All you need to do to the view objects at this point is change the URLs for the forms and links. The form
declaration in the
edit and new views (in app/views/ingredients/edit.html.erb and app/views/
ingredients/new.html.erb
) should now read as follows:
< % form_for([@recipe, @ingredient]) do |f| % >
Again, this implicitly creates the correct URL from the two objects.
You also need to change the URL in the edit page (
app/views/ingredients/edit.html.erb ) as
follows:
< %= link_to ‘Show’, [@recipe, @ingredient] % >
You make the same change to the URL on the index page ( app/views/ingredients/index.html.
erb
), except in this case, ingredient is a loop variable, not an instance variable, so you don t include
the @ sign.
Similarly, you need to change all the named routes by adding the prefix
recipe_ to the method name
and including the
@recipe variable in the argument list. The link to the index page, accessed via the
back link on several pages in
app/views/ingredients should be changed to this:
< %= link_to ‘Back’, recipe_ingredients_path(@recipe) % >
You also need to make changes to the other named links. Here are some examples:
< %= link_to ‘Edit’, edit_recipe_ingredient_path(@recipe, @ingredient) % >
< %= link_to ‘Destroy’, [@recipe, ingredient],
:confirm = > ‘Are you sure?’, :method = > :delete % >
< %= link_to ‘New ingredient’, new_recipe_ingredient_path(@recipe) % >
At this point, all your tests should run cleanly. If not, an error message will likely be displayed, showing
you exactly which method name change you missed. When you make the analogous change in the edit
view, note that the edit link in the
index.html.erb page does not include the @ sign for the ingredient,
as it is a loop variable, not an instance variable.
c01.indd 20c01.indd 20 1/30/08 4:02:26 PM1/30/08 4:02:26 PM