passing a variable to a new subroutine route
PLEASE IGNORE MY POST SENT MOMENTS AGO. I WAS NOT FINISHED TYPING. I have the following post that gets a user's selection from a page then looks up the corresponding records for the selection and put them into a hash refrence. THat part works fine. How can I then call another page/route with that hash_refrence? For example; post '/mypage' => sub { # let user make a selection my $select_id = params->{Sselect}; print "\n\nLooks Like They Selected No: $select_id\n\n"; # look up DB records corresponding to $select_id; # store them in hash refrence $sel_ref } Now I would like to call template/page sport_add but I would like it to have $sel_ref so that it could put that data into the template sport_add get '/sport_add' => sub { template 'sport_add.tt', { 'MyTypes' => $sel_ref, # 'Testing' => "This is text to test to make sure this will come through", # to test, use this like <% Testing %> on your template page }, {}; }; Thanks for any help. Have a great afternoon.
2015-08-23 22:31 GMT+03:00 Richard Reina <gatorreina@gmail.com>:
PLEASE IGNORE MY POST SENT MOMENTS AGO. I WAS NOT FINISHED TYPING.
I think, in your previous post you were much closer to the needed pattern.
post '/mypage' => sub { # let user make a selection
my $select_id = params->{Sselect};
print "\n\nLooks Like They Selected No: $select_id\n\n";
Printing here is not good idea.
# look up DB records corresponding to $select_id;
# store them in hash refrence $sel_ref
Nope. If you need to store users posted data in DB, you redirect them afterwards to another route (/sport_add/$sele) and look up from DB there (get your $self_ref). If you don't need to store, you don't have need for post-route at all. And your get-route would look like: get '/sport_add/:id' => sub { my ( $id ) = splat; # DB query by ID # template call ... } Hope it helps a bit wbr, -- Kõike hääd, Gunnar
Hi WK, Thank you very much for the reply. Before I saw this came up with this solution to pass the variable to the other sub/template: # take them to the add_sport (but with the id no) hook 'before' => sub { var S_id => $select_id; request->path('/sport_add') }; redirect '/sport_add'; It actually worked but I was wondering if doing it this way with hook and redirect is a bad idea? Also, I was wondering why it's a bad idea to print input from dancer to the console? Does it produce a security vulnerability or is it merely disruptive to dancer? Thanks 2015-08-23 18:08 GMT-05:00 WK <wanradt@gmail.com>:
2015-08-23 22:31 GMT+03:00 Richard Reina <gatorreina@gmail.com>:
PLEASE IGNORE MY POST SENT MOMENTS AGO. I WAS NOT FINISHED TYPING.
I think, in your previous post you were much closer to the needed pattern.
post '/mypage' => sub { # let user make a selection
my $select_id = params->{Sselect};
print "\n\nLooks Like They Selected No: $select_id\n\n";
Printing here is not good idea.
# look up DB records corresponding to $select_id;
# store them in hash refrence $sel_ref
Nope. If you need to store users posted data in DB, you redirect them afterwards to another route (/sport_add/$sele) and look up from DB there (get your $self_ref).
If you don't need to store, you don't have need for post-route at all.
And your get-route would look like:
get '/sport_add/:id' => sub { my ( $id ) = splat; # DB query by ID # template call ... }
Hope it helps a bit
wbr, -- Kõike hääd,
Gunnar _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
2015-08-24 18:52 GMT+03:00 Richard Reina <gatorreina@gmail.com>:
Thank you very much for the reply. Before I saw this came up with this solution to pass the variable to the other sub/template:
# take them to the add_sport (but with the id no) hook 'before' => sub { var S_id => $select_id; request->path('/sport_add') };
redirect '/sport_add';
You may need before-hook sometimes, but not now (if I understood you correctly). before-hook is executed on every request. Mostly you don't have need to set id such way for every route. For basic data submitting is best to submit data to post-route and after successful data-saving redirect to result page. Like this: post '/sport_add' => sub { my $id = params->{ id }; # DO something to store needed data redirect "/sport_add/$id"; }; get '/sport_add/:id' => sub { my ( $id ) = params->{ id }; template 'sport_add', { id => $id }; };
It actually worked but I was wondering if doing it this way with hook and redirect is a bad idea?
The before-hook was redundant there, redirect was right way. In cookbook is shown one way to use it: http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod#Before_hooks_-_pr...
Also, I was wondering why it's a bad idea to print input from dancer to the console? Does it produce a security vulnerability or is it merely disruptive to dancer?
Have you tried to print there? You print into SТDOUT and you ruin http-headers, resulting faulty page. print does not send anything to console, use log-methods for it (like debug). Wbr, -- Kõike hääd, Gunnar
Hi WK, Thanks again for the reply. When I try it your way: redirect "/sport_add/$id"; Dancer attempts to serve up the page /sport_add/105 (105 being an the value of $ID) and of course gets an error. Not sure what I am doing wrong. Also, are you saying that hook before makes the var S_id global? I have tried invoking it briefly in other routes and it did not seem to be in scope. Thanks for the help. 2015-08-24 11:45 GMT-05:00 WK <wanradt@gmail.com>:
2015-08-24 18:52 GMT+03:00 Richard Reina <gatorreina@gmail.com>:
Thank you very much for the reply. Before I saw this came up with this solution to pass the variable to the other sub/template:
# take them to the add_sport (but with the id no) hook 'before' => sub { var S_id => $select_id; request->path('/sport_add') };
redirect '/sport_add';
You may need before-hook sometimes, but not now (if I understood you correctly). before-hook is executed on every request. Mostly you don't have need to set id such way for every route. For basic data submitting is best to submit data to post-route and after successful data-saving redirect to result page. Like this:
post '/sport_add' => sub { my $id = params->{ id }; # DO something to store needed data redirect "/sport_add/$id"; };
get '/sport_add/:id' => sub { my ( $id ) = params->{ id }; template 'sport_add', { id => $id }; };
It actually worked but I was wondering if doing it this way with hook and redirect is a bad idea?
The before-hook was redundant there, redirect was right way.
In cookbook is shown one way to use it:
http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod#Before_hooks_-_pr...
Also, I was wondering why it's a bad idea to print input from dancer to the console? Does it produce a security vulnerability or is it merely disruptive to dancer?
Have you tried to print there? You print into SТDOUT and you ruin http-headers, resulting faulty page. print does not send anything to console, use log-methods for it (like debug).
Wbr, -- Kõike hääd,
Gunnar _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
2015-08-24 20:36 GMT+03:00 Richard Reina <gatorreina@gmail.com>:
Thanks again for the reply. When I try it your way:
redirect "/sport_add/$id";
Dancer attempts to serve up the page /sport_add/105 (105 being an the value of $ID) and of course gets an error. Not sure what I am doing wrong.
Did you set up get-route like in my example? It is required to show actual data from database.
are you saying that hook before makes the var S_id global? I have tried invoking it briefly in other routes and it did not seem to be in scope.
before-hook is run on every request, it is not scoped. In your last example you use sigil with ($) var-method. Is it intentional? "var" is is here keyword from Dancer DSL and using sigil here for identifier gets interpolated, I think. Wbr, -- Kõike hääd, Gunnar
participants (2)
-
Richard Reina -
WK