Take the rpp preference out of auth...

and put it in the layout. The form response now redirects back to the current
page. Catch: if on the last page and you increase rpp, it redirects beyond
the last page and therefore 404s.
This commit is contained in:
barnold 2022-09-29 10:50:17 +01:00
parent c6687ad3f5
commit 5cc21a2e7a
3 changed files with 16 additions and 8 deletions

View File

@ -93,6 +93,7 @@ sub startup ($self) {
$r->get('/books/<page_number:num>')->to('book#books')->name('books');
$r->get('/authors/<page_number:num>')->to('book#authors')->name('authors');
$r->get('/author/<id:num>/<page_number:num>')->to('book#author')->name('author');
$r->post('/preferences')->to('auth#preferences')->name('preferences');
# Put a route under athentication.
my $auth = $r->under(
@ -105,7 +106,6 @@ sub startup ($self) {
}
);
$auth->get('/account')->to('auth#account')->name('account');
$auth->post('/preferences')->to('auth#preferences')->name('preferences');
}
1;

View File

@ -38,7 +38,7 @@ sub preferences ($self) {
$self->flash(
confirmation => sprintf("Updated rows per page to %s.", $self->rpp)
);
$self->redirect_to('account');
$self->redirect_to($self->param('target_url'));
}
1;

View File

@ -5,24 +5,32 @@
<link rel="stylesheet" href="/default.css">
</head>
<body>
<div>
<%= content %>
</div>
<hr/>
<div>
<%# Provide a link to the home page, unless we're on it. %>
<%# I gave the home page route a name, else it's an empty string. %>
<% if ('home' ne current_route) { %>
<%= link_to("Home" => 'home') %>
<% } %>
</div>
<div>
<% if (logname) { %>
You are logged in as <%= logname %>.
<%= link_to("Logout" => 'logout') %>
<% } elsif (current_route ne 'auth') { %>
<%= link_to("Login" => 'auth') %>
<% } %>
%= form_for preferences => begin
<%= hidden_field target_url => url_with('current') %>
%= label_for rows_per_page => 'Rows per page'
<%= number_field rows_per_page => $c->rpp,
min => 1, max => max_rpp, maxlength => 4, size => 4 %>
%= submit_button "Update"
% end
</div>
<hr/>
<div>
<%= content %>
</div>
<hr/>
</body>
</html>