Catalyst::Manual::Tutorial::BasicCRUD 0.02 review
DownloadCatalyst::Manual::Tutorial::BasicCRUD is a Catalyst Tutorial - Part 3: Basic CRUD. Include a Create Action in the Books Controller
|
|
Catalyst::Manual::Tutorial::BasicCRUD is a Catalyst Tutorial - Part 3: Basic CRUD.
Include a Create Action in the Books Controller
Edit lib/MyApp/Controller/Books.pm 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('MyAppDB::Book')->create({
title => $title,
rating => $rating
});
# Add a record to the join table for this book, mapping to
# appropriate author
$book->add_to_book_authors({author_id => $author_id});
# Note: Above is a shortcut for this:
# $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;
# This is a hack to disable XSUB processing in Data::Dumper
# (it's used in the view). This is a work-around for a bug in
# the interaction of some versions or Perl, Data::Dumper & DBIC.
# You won't need this if you aren't using Data::Dumper (or if
# you are running DBIC 0.06001 or greater), but adding it doesn't
# hurt anything either.
$Data::Dumper::Useperl = 1;
# Set the TT template to use
$c->stash->{template} = 'books/create_done.tt2';
}
Notice that Catalyst takes "extra slash-separated information" from the URL and passes it as arguments in @_. The url_create action then uses a simple call to the DBIC create method to add the requested information to the database (with a separate call to add_to_book_authors 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.
Requirements:
Perl
Catalyst::Manual::Tutorial::BasicCRUD 0.02 keywords