aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-08-01 13:38:35 +0200
committerHarald Eilertsen <haraldei@anduin.net>2018-08-01 13:38:35 +0200
commit3ef15d64c60d943b64d998449ed0eaa1dee0ac3e (patch)
treea5446f8020e10fc1b0b9662dec04303dd2cdf74d
parentd285d0525f86160a61067942346fb40f2bc5c797 (diff)
downloadrocket-blog-3ef15d64c60d943b64d998449ed0eaa1dee0ac3e.tar.gz
rocket-blog-3ef15d64c60d943b64d998449ed0eaa1dee0ac3e.tar.bz2
rocket-blog-3ef15d64c60d943b64d998449ed0eaa1dee0ac3e.zip
Fix display of flash messages.
Pass full flash to template, and style according to flash name/type.
-rw-r--r--public/assets/application.css13
-rw-r--r--src/controllers/home_controller.rs2
-rw-r--r--src/controllers/login_controller.rs2
-rw-r--r--src/controllers/users_controller.rs4
-rw-r--r--src/utils/mod.rs6
-rw-r--r--templates/layout.html2
6 files changed, 21 insertions, 8 deletions
diff --git a/public/assets/application.css b/public/assets/application.css
index 299be36..3063805 100644
--- a/public/assets/application.css
+++ b/public/assets/application.css
@@ -36,3 +36,16 @@ form {
.actions {
display: block;
}
+
+.flash {
+ padding: 0.5em;
+ border: 1px dashed darkgray;
+}
+
+.error {
+ background-color: #f74c4c;
+}
+
+.success {
+ background-color: lightgreen;
+}
diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs
index bef47de..38db52a 100644
--- a/src/controllers/home_controller.rs
+++ b/src/controllers/home_controller.rs
@@ -16,7 +16,7 @@ implement_responder_for!(IndexTemplate);
fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> utils::Page<IndexTemplate> {
utils::Page {
title: String::from("Bloggen"),
- flash: flash.map_or(None, |f| Some(f.msg().to_string())),
+ flash: flash,
content: IndexTemplate {
posts: models::Post::get_all(conn).into_iter()
.map(|p| ShowPostTemplate { post: p })
diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs
index 800a6b2..564deff 100644
--- a/src/controllers/login_controller.rs
+++ b/src/controllers/login_controller.rs
@@ -14,7 +14,7 @@ implement_responder_for!(LoginTemplate);
pub fn new(flash: Option<rocket::request::FlashMessage>) -> utils::Page<LoginTemplate> {
utils::Page {
title: String::from("Log in"),
- flash: flash.map_or(None, |f| Some(f.msg().to_string())),
+ flash: flash,
content: LoginTemplate{}
}
}
diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs
index a3281de..91cfc5c 100644
--- a/src/controllers/users_controller.rs
+++ b/src/controllers/users_controller.rs
@@ -15,7 +15,7 @@ implement_responder_for!(UsersTemplate);
fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> utils::Page<UsersTemplate> {
utils::Page {
title: String::from("Users"),
- flash: flash.map_or(None, |f| Some(f.msg().to_string())),
+ flash: flash,
content: UsersTemplate {
users: ::models::User::all(conn).unwrap()
}
@@ -67,7 +67,7 @@ implement_responder_for!(NewUserTemplate);
fn new(flash: Option<rocket::request::FlashMessage>) -> utils::Page<NewUserTemplate> {
utils::Page {
title: String::from("New user"),
- flash: flash.map_or(None, |f| Some(f.msg().to_string())),
+ flash: flash,
content: NewUserTemplate {
user: Default::default()
}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 57975bc..5392224 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -14,7 +14,7 @@ pub fn init_db_pool<'a>(dburl: &'a str) -> Pool {
use std::ops::Deref;
use rocket::http::Status;
-use rocket::request::{self, FromRequest};
+use rocket::request::{self, FlashMessage, FromRequest};
use rocket::{Request, State, Outcome};
// Connection request guard type: a wrapper around an r2d2 pooled connection.
@@ -44,11 +44,11 @@ impl Deref for DbConn {
}
}
-#[derive(BartDisplay, Serialize)]
+#[derive(BartDisplay)]
#[template = "templates/layout.html"]
pub struct Page<T: Display> {
pub title: String,
- pub flash: Option<String>,
+ pub flash: Option<FlashMessage>,
pub content: T
}
diff --git a/templates/layout.html b/templates/layout.html
index 4eeac80..887a2b7 100644
--- a/templates/layout.html
+++ b/templates/layout.html
@@ -8,7 +8,7 @@
<header class="page-header">
<h1>{{ title }}</h1>
</header>
- {{#flash}}<div class="flash">{{ . }}</div>{{/flash}}
+ {{# flash }}<div class="flash {{ .name() }}">{{ .msg() }}</div>{{/ flash }}
{{{ content }}}
</body>
</html>