Changed reference to "user base" into "use parent" to match code
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / BasicCRUD.pod
index 0aa7e2c..b6afd45 100644 (file)
@@ -1,4 +1,4 @@
-=head1 NAME
+ =head1 NAME
 
 Catalyst::Manual::Tutorial::BasicCRUD - Catalyst Tutorial - Part 4: Basic CRUD
 
@@ -620,7 +620,7 @@ and 2) the four lines for the Delete link near the bottom).
         </td>
         <td>
           [% # Add a link to delete a book %]
-          <a href="[% c.uri_for('id', book.id, 'delete') %]">Delete</a>
+          <a href="[% c.uri_for(c.controller.action_for('delete'), [book.id]) %]">Delete</a>
         </td>
       </tr>
     [% END -%]
@@ -630,6 +630,33 @@ The additional code is obviously designed to add a new column to the
 right side of the table with a C<Delete> "button" (for simplicity, 
 links will be used instead of full HTML buttons).
 
+Also notice that we are using a more advanced form of C<uri_for> than 
+we have seen before.  Here we use C<$c-E<gt>controller-
+E<gt>action_for> to automatically generate a URI appropriate for that 
+action based on the method we want to link to while inserting the 
+C<book.id> value into the appropriate place.  Now, if you ever change 
+C<:PathPart('delete')> in your controller method to 
+C<:PathPart('kill')>, then your links will automatically update 
+without any changes to your .tt2 template file.  As long as the name 
+of your method does not changed ("delete" here), then your links will 
+still be correct.  There are a few shortcuts and options when using
+C<action_for()>:
+
+=over 4
+
+=item *
+
+If you are referring to a method in the current controller, you can
+use C<$self-E<gt>action_for('_method_name_')>.
+
+=item *
+
+If you are referring to a method in a different controller, you need
+to include that as an argument to C<controller()>, as in
+C<$c-E<gt>controller('_controller_name_')-E<gt>action_for('_method_name_')>.
+
+=back
+
 B<Note:> You should use more than just a simple link with your 
 applications. Consider using some sort of of confirmation page 
 (typically with unique actions in your controller for both the 
@@ -655,6 +682,13 @@ operate on an existing book can chain directly off base.
 To add the C<object> method, edit C<lib/MyApp/Controller/Books.pm>
 and add the following code:
 
+    =head2 object
+    
+    Fetch the specified book object based on the book ID and store
+    it in the stash
+    
+    =cut
+    
     sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
         my ($self, $c, $id) = @_;
         
@@ -805,8 +839,9 @@ C<sub delete> method to match:
         # Set a status message to be displayed at the top of the view
         $c->stash->{status_msg} = "Book deleted.";
     
-        # Redirect the user back to the list page
-        $c->response->redirect($c->uri_for('/books/list'));
+        # Redirect the user back to the list page.  Note the use
+        # of $self->action_for as earlier in this section (BasicCRUD)
+        $c->response->redirect($c->uri_for($self->action_for('list'));
     }
 
 
@@ -848,7 +883,7 @@ method to match the following:
         $c->stash->{object}->delete;
     
         # Redirect the user back to the list page with status msg as an arg
-        $c->response->redirect($c->uri_for('/books/list', 
+        $c->response->redirect($c->uri_for($self->action_for('list'), 
             {status_msg => "Book deleted."}));
     }