[Catalyst] OT: edit/add seperate actions?

Bill Moseley moseley at hank.org
Tue Mar 3 17:38:29 GMT 2009


On Tue, Mar 03, 2009 at 03:33:51PM +0000, Iain wrote:
> # edit
> /app/notice/*/edit (POST Back to) /app/notice/*/edit
> 
> Actually, I like option 2 best. So I suppose I'm asking for reasons why
> we shouldn't use option 1 :-)

Here's what I often do, but it doesn't used chained actions, the URL
is backwards (foo/edit/$id instead of foo/$id/edit) and is *not* the
recommended approach.  It also uses a plugin when it probably should
be a controller base class (but, since it's used in almost every
controller the plugin is handy).

But, I keep doing it because it's just so damn easy.


I have lots and lots of simple create/edit controllers and they all
look basically like this:


sub edit : Local {
    my ( $self, $c, $id ) = @_;

    $c->post_redirect( 'list' ) if $c->update_from_form( $id );
}


Of course, update_from_form() figures out what "form" object to use
based on the action, and the form object knows what model object is
associated and how to validate the input parameters and do any extra
magic that might be needed when updating or creating.

If $id is undefined and the form is valid then a new thing is created.
If $id is defined and valid then the thing is updated.  If $id is not
valid (for whatever reason that the form object decides upon), or the
request is not a POST or the form isn't valid then the form is
displayed to the user (perhaps with an error message).  I often will
handle invalid $id in a more customized way per controller.

And post_redirect can be passed a message that is cached for redisplay
after the redirect.


In this app we use XMLRPC for the API, so to expose that action it
might be like this:

sub edit : Local XMLRPC( 'user.add', 'user.update', 'user.get' ) {
    my ( $self, $c, $id ) = @_;

    $c->post_redirect( 'list', 'Foo' ) if $c->update_from_form( $id );
}

So the same form validation is used for create and update via the API,
and the xmlrpc code knows how to wrap that action to handle the
specifics of setting up the add, update, and get actions.



-- 
Bill Moseley
moseley at hank.org
Sent from my iMutt




More information about the Catalyst mailing list