[dancer-users] updating a form

David Precious davidp at preshweb.co.uk
Mon Feb 10 14:18:03 GMT 2014


I'd do what James said - change your template so that the form submits
to /edit/:id (e.g. <form action=/edit/42" method="post">), then change
your edit route to post '/edit/:id' => sub { ... }

That'll handle it nicely, and is a bit more RESTful, too.

Also, random comments:

In the get route, you load the data straight from a YAML file, but in
the post route, you call get_AoH_imagelist() to get it - incomplete
refactor, perhaps?

When you say:

    my $data = get_AoH_imagelist();
    my @data = @{$data}

You could condense that to:

   my @data = @{ get_AoH_imagelist() };



On Mon, 10 Feb 2014 14:37:29 +0100
Gert van Oss <gertvanoss at me.com> wrote:

> Hi, 
> 
> I’m trying to build a small app to comment on images. Probably I’m
> almost there but currently stuck with updating a ‘file.yml' by a
> html-form. 
> 
> I’ve made two routes (shown below) “get ‘/:id/edit’ for showing the
> form with the particular image to comment on. When hitting save the
> ‘post ‘/edit’ will be called. My problem is that the post route
> doesn’t have the $id initialised. Is there someone around who can
> tell me how to solve this or point to me what I’m doing wrong?
> 
> Thanks,
> Gert
> 
> The structure is an AoH. 
> The file.yml is like
> 
> ---
> - collection: ''
>   description: good
>   id: 0
>   imgf: photo.jpg
>   tags: ''
> - collection: ''
>   description: ''
>   etc..
> 
> ————end of yaml
> 
> 
> #---------------------------------------------------------------------
> #
> # get EDIT
> #
> #---------------------------------------------------------------------
> get '/:id/edit' => sub {
>     my $filename =
>       Dancer::FileUtils::read_file_content(
>         Dancer::FileUtils::path( setting('appdir'), 'myImages.yml' )
> ); my $data = from_yaml $filename;
>     my @data = @{$data};
> 
>     #print Dumper \@data;
>     my $id = param('id');
>     template 'edit', { data => $data[$id], id => $id, };
> };
> 
> #---------------------------------------------------------------------
> #
> # post EDIT
> #
> #---------------------------------------------------------------------
> 
> post '/edit' => sub {
>     my $data = get_AoH_imagelist();
>     my @data = @{$data};
>     my $id   = param('id');
>     $data[$id] { description}=  params->{description} ;
>     #$data[2] { description}=  params->{description} ; 
>     # this works but then all descriptions end up in the third group
> of hashes. my $filename = "myImages.yml";
>     write_file $filename, to_yaml($data);
>     redirect '/';
> };
> 



-- 
David Precious ("bigpresh") <davidp at preshweb.co.uk>
http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan        www.preshweb.co.uk/github




More information about the dancer-users mailing list