X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F04_BasicCRUD.pod;h=3e7e02ebe5ceac136b31c7ec36ef0f5d839cf16e;hp=4542b50f11ebf7d1a7122b4ce72c2e737424ed47;hb=fce83e5f2a2da9f9117562d05aec1e161cc3c109;hpb=3ab6187c1a123983b6ae29e57f543328ce15755c diff --git a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod index 4542b50..3e7e02e 100644 --- a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod @@ -91,34 +91,34 @@ based submission in the sections that follow). Edit C and enter the following method: =head2 url_create - + Create a book with the supplied title, rating, and author - + =cut - + sub url_create : Local { # In addition to self & context, get the title, rating, & # author_id args from the URL. Note that Catalyst automatically # puts extra information after the "//model('DB::Book')->create({ title => $title, rating => $rating }); - + # Add a record to the join table for this book, mapping to # appropriate author - $book->add_to_book_author({author_id => $author_id}); + $book->add_to_book_authors({author_id => $author_id}); # Note: Above is a shortcut for this: - # $book->create_related('book_author', {author_id => $author_id}); - + # $book->create_related('book_authors', {author_id => $author_id}); + # Assign the Book object to the stash for display in the view $c->stash->{book} = $book; - + # Set the TT template to use $c->stash->{template} = 'books/create_done.tt2'; } @@ -127,7 +127,7 @@ Notice that Catalyst takes "extra slash-separated information" from the URL and passes it as arguments in C<@_>. The C action then uses a simple call to the DBIC C method to add the requested information to the database (with a separate call to -C to update the join table). As do virtually all +C to update the join table). As do virtually all controller methods (at least the ones that directly handle user input), it then sets the template that should handle this request. @@ -140,33 +140,27 @@ Edit C and then enter: [% # Not a good idea for production use, though. :-) 'Indent=1' is -%] [% # optional, but prevents "massive indenting" of deeply nested objects -%] [% USE Dumper(Indent=1) -%] - + [% # Set the page title. META can 'go back' and set values in templates -%] [% # that have been processed 'before' this template (here it's for -%] [% # root/lib/site/html and root/lib/site/header). Note that META only -%] [% # works on simple/static strings (i.e. there is no variable -%] [% # interpolation). -%] [% META title = 'Book Created' %] - - [% # Output information about the record that was added. First title. -%] + + [% # Output information about the record that was added. First title. -%]

Added book '[% book.title %]' - - [% # Output the last name of the first author. This is complicated by an -%] - [% # issue in TT 2.15 where blessed hash objects are not handled right. -%] - [% # First, fetch 'book.author' from the DB once. -%] - [% authors = book.author %] - [% # Now use IF statements to test if 'authors.first' is "working". If so, -%] - [% # we use it. Otherwise we use a hack that seems to keep TT 2.15 happy. -%] - by '[% authors.first.last_name IF authors.first; - authors.list.first.value.last_name IF ! authors.first %]' - + + [% # Output the last name of the first author. -%] + by '[% book.authors.first.last_name %]' + [% # Output the rating for the book that was added -%] with a rating of [% book.rating %].

- + [% # Provide a link back to the list page -%] [% # 'uri_for()' builds a full URI; e.g., 'http://localhost:3000/books/list' -%]

Return to list

- + [% # Try out the TT Dumper (for development only!) -%]
     Dump of the 'book' variable:
@@ -180,6 +174,22 @@ L "pretty printing" of objects and
 variables.  Other than that, the rest of the code should be familiar
 from the examples in Chapter 3.
 
+Note: If you are using TT v2.15 you will need to change the code that 
+outputs the "last name for the first author" above to match this:
+
+    [% authors = book.authors %]
+    by '[% authors.first.last_name IF authors.first;
+           authors.list.first.value.last_name IF ! authors.first %]'
+
+to get around an issue in TT v2.15 where blessed hash objects were not 
+handled correctly.  But, if you are still using v2.15, it's probably 
+time to upgrade  (v2.15 is exactly 3 years old on the day I'm typing 
+this).  If you are following along in Debian, then you should be on at 
+least v2.20.  You can test your version of Template Toolkit with the 
+following:
+
+    perl -MTemplate -e 'print "$Template::VERSION\n"'
+
 
 =head2 Try the 'url_create' Feature
 
@@ -215,14 +225,18 @@ The C statements are obviously adding the book and linking it to
 the existing record for Richard Stevens.  The C