docdurdee has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, I am dipping my feet into all things webapp with a simple application. I would like to build web app to render a list of files in a filesystem directory and attach and actionable button to each one. In the attempt below, the get method sends the list of files to the stash and then renders the list of files/button as expected. The trouble seems to be in how the button is templated. How can the button be set up to attach the filename to the button which sends it off to another method on the click? Eventually, I would like to get resulting output posted into a frame on the righthand side of the page with the list of files remaining on the left and side; I know this will take a few more steps, but for now I am stuck. Thanks in advance.
#!/usr/bin/env perl use Mojolicious::Lite; use Path::Tiny; plugin 'TagHelpers'; get '/' => sub{ my $c = shift; my $path = path("/Foo/Bar/Baz"); $c->stash(filepath => $path, files => [$path->children(qr/\.txt/)] + ); $c->render('index'); }; post '/' => sub{ my $c = shift; my $file = $c->param('id'); print $file; # print to console $c->render(text => $file); }; app->start; __DATA__ @@ index.html.ep % layout 'default'; % title $filepath ; <h1>Let's test the files in <%= $filepath %>!</h1> <table border="1"> <tr> <th>File</th> <th>Action</th> </tr> % foreach my $file (@$files) { <tr><td><%= $file %></td> <td> %= form_for '/' => (method => 'POST') => begin %= submit_button 'validate', id => $file % end </td> </tr> % } </table> @@ layouts/default.html.ep <!DOCTYPE html> <html> <head><title><%= title %></title></head> <body><%= content %></body> </html>
EDIT: I think I figured it out the first step. I got rid of the post command, and added a new get with a route placeholder:
get '/validate/*filename' => sub{ my $c = shift; print $c->param('filename'),"\n\n\n\n"; # print to console $c->render('index'); };
Then I mixed a little JQuery into the button:
<input type='button' value='validate' onclick = "$.get('/validate/<%= +$file %>');">
And that sends back the file name. Very cool. Thanks in advance if you point out a better way or provide any other tips.

Replies are listed 'Best First'.
Re: Mojolicious to and from buttons
by Anonymous Monk on Aug 29, 2016 at 22:47 UTC

    How can the button be set up to attach the filename to the button which sends it off to another method on the click?

    The program you have posted already does that