Let's start by writing the easiest method, a method to delete a person from the database. All we need is a simple action in the Person
Controller. (Note: The order of the methods in the Person.pm
file does not matter. You can add this method before or after sub index{ }
):
sub delete : Local { my ($self, $c, $id) = @_; my $person = $c->model('AddressDB::People')-> find({id => $id}); $c->stash->{person} = $person; if($person){ $c->stash->{message} = 'Deleted '. $person->name; $person->delete; } else { $c->response->status(404); $c->stash->{error} = "No person $id"; } $c->forward('list'); }
This action will create a URL that looks like /person/delete/1
, where 1
is the person's ID number in the database. Using that ID number that's passed in the URL, we look for the row in the database with that ID. If we find one, we set the status message to Deleted Person's Name
and then delete the person (the delete method...