Render flash messages

This commit is contained in:
Sam Samai 2021-09-02 22:30:23 +10:00
parent 5f47421b38
commit 39b5cb7f33
3 changed files with 18 additions and 9 deletions

View File

@ -4,6 +4,7 @@ extern crate rocket;
use rocket::fairing::{self, AdHoc};
use rocket::form::{Context, Form};
use rocket::fs::{relative, FileServer};
use rocket::request::FlashMessage;
use rocket::response::{Flash, Redirect};
use rocket::serde::json::Json;
use rocket::{Build, Request, Rocket};
@ -46,18 +47,20 @@ async fn create(conn: Connection<Db>, post_form: Form<post::Model>) -> Flash<Red
}
#[get("/")]
async fn list(conn: Connection<Db>) -> Template {
async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template {
let posts = Post::find()
.all(&conn)
.await
.expect("could not retrieve posts")
.into_iter()
.collect::<Vec<_>>();
let flash = flash.map(FlashMessage::into_inner);
Template::render(
"index",
context! {
posts: posts,
flash: flash,
},
)
}

View File

@ -2,24 +2,24 @@
border: 1px solid #ff0000 !important;
}
.field-error-msg {
.field-error-flash {
color: #ff0000;
display: block;
margin: -10px 0 10px 0;
}
.field-success {
border: 1px solid #5AB953 !important;
border: 1px solid #5ab953 !important;
}
.field-success-msg {
color: #5AB953;
.field-success-flash {
color: #5ab953;
display: block;
margin: -10px 0 10px 0;
}
span.completed {
text-decoration: line-through;
text-decoration: line-through;
}
form.inline {
@ -29,7 +29,7 @@ form.inline {
form.link,
button.link {
display: inline;
color: #1EAEDB;
color: #1eaedb;
border: none;
outline: none;
background: none;
@ -45,8 +45,9 @@ button.link {
letter-spacing: inherit;
}
form.link:hover, button.link:hover {
color: #0FA0CE;
form.link:hover,
button.link:hover {
color: #0fa0ce;
}
button.small {

View File

@ -1,5 +1,10 @@
{% extends "base" %} {% block content %}
<h1>Posts</h1>
{% if flash %}
<small class="field-{{ flash.0 }}-flash">
{{ flash.1 }}
</small>
{% endif %}
<table>
<tbody>
<thead>