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=f7f5f1b76371adf1d3cce17568a1ece5c8e1f074;hp=4542b50f11ebf7d1a7122b4ce72c2e737424ed47;hb=4768184b3b277399116fbd53cae3697a9767fee5;hpb=3ab6187c1a123983b6ae29e57f543328ce15755c diff --git a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod index 4542b50..f7f5f1b 100644 --- a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod @@ -68,9 +68,9 @@ functionality, will be addressed in Chapter 9. Although this chapter of the tutorial will show you how to build CRUD functionality yourself, another option is to use a "CRUD builder" type -of tool to automate the process. You get less control, but it's quick -and easy. For example, see -L, +of tool to automate the process. You get less control, but it can be +quick and easy. For example, see +L, L, and L. @@ -91,43 +91,41 @@ 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 { + + 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}); - - # 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'; + # $book->create_related('book_authors', {author_id => $author_id}); + + # Assign the Book object to the stash for display and set template + $c->stash(book => $book, + template => 'books/create_done.tt2'); } 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 +138,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,24 +172,32 @@ 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 almost 4 years old).  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
 
-If the application is still running from before, use C to kill
-it. Then restart the server:
+Make sure the development server is running with the "-r" restart
+option:
 
-    $ DBIC_TRACE=1 script/myapp_server.pl
+    $ DBIC_TRACE=1 script/myapp_server.pl -r
 
 Note that new path for C appears in the startup debug
 output.
 
-B: You can use C