Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Dancer & Plackup - Redirect not refreshing page data

by Anonymous Monk
on Aug 08, 2014 at 00:50 UTC ( [id://1096695]=note: print w/replies, xml ) Need Help??


in reply to Re: Dancer & Plackup - Redirect not refreshing page data
in thread Dancer & Plackup - Redirect not refreshing page data

Here is the test case: Controller:
package Test::TestController; use Dancer ':syntax'; use strict; use Test::Model::Test; our $VERSION = '0.1'; prefix '/test'; route(); sub route { hook 'before' => sub { if (! session('user') && request->path_info =~ /^\/test\// && +request->path_info !~ m{^/login}) { var requested_path => request->path_info; request->path_info('/test/login'); } }; get '/login' => sub { template 'login_test.tt', { }; }; ##log user in. Validate authentication then redirect to user base +route post '/login' => sub { session user => {id => 1, role =>{ id => 1} }; redirect '/test/website/get/1'; }; get '/website/get/:id' => sub { ##check we're not being passed non id stuff unless (params->{id} =~ /^[\d]+$/) { redirect '/test/login'; r +eturn } my $website = Test::Model::Test::get_website(params->{id}); + ##only for admin for all websites ##check that the website is owned by this user otherwise unless (session('user')->{role}->{id} eq Test::Model::Test::RO +LE_ADMIN || $website->{created_by} eq session('user')->{id}) { redirect '/login'; } template 'website_test.tt', { 'values' => $website, 'form_url' => '/test/website/edit/'.params->{id}, }; }; post '/website/edit/:id' => sub { ##check we're not being passed non id stuff unless (params->{id} =~ /^[\d]+$/) { redirect '/test/login'; r +eturn } my $website = Test::Model::Test::get_website(params->{id}); + ##only for admin for all websites ##check that the website is owned by this user otherwise unless (session('user')->{role}->{id} eq Test::Model::Test::RO +LE_ADMIN || $website->{created_by} eq session('user')->{id}) { redirect '/login'; } my $param_ref = params; Test::Model::Test::edit_website(session('user'), $param_ref); ##Redirect to add a new website with a flash message #flash message => 'Website successfully edited!'; redirect '/test/website/get/'.params->{id}; }; } true;
Model
package Test::Model::Test; use Dancer::Plugin::Database; use Dancer::Logger; use constant ROLE_ADMIN => 1; ##Edits a new website sub edit_website($$) { my ($user, $website) = @_; database->quick_update('test_website', {id => $website->{id}}, { name => $website +->{name}, url => $website- +>{url}, }); database->commit(); } ##Return sthe website object . sub get_website($) { my ($id) = @_; my $website = database->quick_select('test_website', { id => $id } +); return $website; } true;
Template
<form action="<% form_url %>" method="post" id="f-submit-form" enctype +="multipart/form-data"> <fieldset> <h2>Edit Website</h2> <ul> <li id="f-container-website-name"> <label for="f-website-name">Name</label> <input type="text" name="name" id="f-website-name" <%IF va +lues.name %>value="<% values.name %>"<% END %> /> </li> <li id="f-container-website-url"> <label for="f-website-url">Url</label> <input type="text" name="url" id="f-website-url" <%IF valu +es.url %>value="<% values.url %>"<% END %> /> </li> <li id="f-container-submit"> <button value="Submit" class="maia-button" id="sub +mit" type="submit">Submit</button> </li> </ul> </form>
Login template
<div class="login-container"> <div class="login-header"> Login </div> <% IF err %> <div class="login-error-container-enabled">Incorrect username +/ password</div> <% END %> <div class="login-form-container"> <form id="f-form-login" action="<% request.uri_base %>/test/lo +gin" method="post" enctype="multipart/form-data"> <fieldset> <ul> <li id="f-container-username"> <label for="f-username">Username</label> <input type="text" name="username" id="f-usern +ame"/> </li> <li id="f-container-password"> <label for="f-password">Password</label> <input type="password" name="password" id="f-p +assword"/> </li> <li id="f-container-submit"> <button value="Submit" class="button" id="subm +it" type="submit">LOGIN</button> </li> </ul> </fieldset> </form> </div> </div>
DB
create table if not exists `testdb`.test_website ( id integer primary key auto_increment, name varchar(255) not null, url varchar(255) not null ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `testdb`.test_website(name, url) values ('test1', 'mytest. +com');

Steps:

  • Login (any user/pwd will do for the test)
  • In /website/get/1 , change the name
  • Click Submit
  • See the name is not refreshed
  • F5, name is refreshed

Above steps work fine when running perl bin/app.pl
Test fails when running
sudo -u nginx /usr/local/bin/plackup -E production -s Starman --workers=2 -l /tmp/plack.sock -a bin/app.pl &

Hopefully somebody can point to what I'm doing wrong in my code. Thanks for taking the time.

Replies are listed 'Best First'.
Re^3: Dancer & Plackup - Redirect not refreshing page data
by choroba (Cardinal) on Aug 08, 2014 at 06:24 UTC
    Can you also show config.yml and production.yml?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1096695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found