[Catalyst] Create Issue with CRUD application
Blaine Everingham
grandmasterblaine at hotmail.com
Tue Aug 21 15:42:22 GMT 2012
Hi,
I'm new to the Catalyst architecture and have been working through some exa=
mples to learn the ropes. However, in the application I am working on, I am=
able to Update, but not Create a new record. I am using HTML::FormHandler =
and I believe that the inability to Create a new record has something to do=
with my setup of the Form Model. Each time I try to create a new record I =
get the following error.
Caught exception in Signup::Web::Controller::users->add "Can't call method =
"resolve" on an undefined value at C:/strawberry/perl/site/lib/DBIx/Class/R=
ow.pm line 1289."The error leads me to believe that the new_result is not w=
orking property with the Form, because if I remove the code and leave the "=
item" empty the form will generate but of course not allow the new record t=
o be added. Data::Dumper shows that the Item variable looks like this:
Item: $VAR1 =3D bless( {}, 'Signup::Web::Model::signup::User' );
Any idea where I have went wrong so that this error is created when adding =
new Records and not updates?
---- HFH Form Object----package Signup::Web::Form::User;
use HTML::FormHandler::Moose;
use namespace::autoclean;
extends 'HTML::FormHandler::Model::DBIC';
has '+item_class' =3D> ( default =3D>'Signup::User' );
has_field 'alias';
has_field 'name_first' =3D> ( type =3D> 'Text', 'label' =3D> 'First Name' =
);
has_field 'name_last' =3D> ( type =3D> 'Text', 'label' =3D> 'Last Name' );
has_field 'email' =3D> ( type =3D> 'Text', required =3D> 1 );
has_field 'submit' =3D> ( type =3D> 'Submit', value =3D> 'Submit' );
__PACKAGE__->meta->make_immutable;
1;
---- User Controller Outlining the "Add" actionpackage Signup::Web::Control=
ler::users;
use Moose;
use namespace::autoclean;
use Signup::Web::Form::User;
BEGIN {extends 'Catalyst::Controller'; }
=3Dhead1 NAME
Signup::Web::Controller::users - Catalyst Controller
=3Dhead1 DESCRIPTION
Catalyst Controller.
=3Dhead1 METHODS
=3Dcut
=3Dhead2 index
=3Dcut
sub index :Path('/users') :Args(0) {
my ( $self, $c ) =3D @_;
=
$c->load_status_msgs;
my $users =3D [ $c->model('Signup::User')->all ];
my @columns =3D ( 'namelast', 'email' );
$c->stash( users =3D> $users, columns =3D> \@columns,
template =3D> 'users/list.tt2' );
=
}
sub add : Local Args(0) {
my ( $self, $c ) =3D @_;
## Store the empty record in the "record" variable
## for $self->form
=
return $self->form( $c );
=
}
sub edit : Local Args(1) {
my ( $self, $c, $user_id ) =3D @_;
## Store the user that we are going to edit =
$c->stash( record =3D> $c->model('Signup::User')->find($user_id) );
=
$c->log->debug("RECORD: edit");
return $self->form($c);
}
sub form : Private{
my ( $self, $c ) =3D @_;
## Check if a record is in the stash, if not create an empty result
my $item =3D $c->stash->{record} || $c->model('Signup::User')->new_result(=
{});
$c->log->debug( "Item: " . Data::Dumper->Dump([ $item ]));
## Create a new form object with the proper Item data
my $form =3D Signup::Web::Form::User->new( item =3D> $item );
=
$c->log->debug( "After new form" );
=
## Set the output template and variables required
$c->stash( template =3D> 'users/form.tt2', form =3D> $form );
$c->log->debug( "Before Process" );
## Validate the form =
return unless $form->process( params =3D> $c->req->parameters );
## On Succcessfull Validation redirect to the index page
$c->response->redirect($c->uri_for($self->action_for('index'),
{mid =3D> $c->set_status_msg("User Added")}
));
=
=
}
=3Dhead1 AUTHOR
A clever guy
=3Dhead1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=3Dcut
__PACKAGE__->meta->make_immutable;
1;
=
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120821/fbd9e=
e37/attachment.htm
More information about the Catalyst
mailing list