From bf2e515855ffa04c8884a14c872001dd378d673e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 14 Jan 2018 23:21:10 +0100 Subject: Add flash messages when adding/editing/deleting posts. --- src/controllers/home_controller.rs | 3 ++- src/controllers/posts_controller.rs | 17 ++++++++++------- src/utils/mod.rs | 1 + templates/layout.html | 1 + 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index 6423a30..d9a539a 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -12,9 +12,10 @@ struct IndexTemplate { implement_responder_for!(IndexTemplate); #[get("/", format = "text/html")] -fn index(conn: utils::DbConn) -> utils::Page { +fn index(flash: Option, conn: utils::DbConn) -> utils::Page { utils::Page { title: String::from("Bloggen"), + flash: flash.map_or(None, |f| Some(f.msg().to_string())), content: IndexTemplate { posts: models::Post::get_all(conn) } diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 38618d3..5fdd60e 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -1,5 +1,5 @@ use rocket::request::Form; -use rocket::response::Redirect; +use rocket::response::{Flash, Redirect}; use utils; #[derive(BartDisplay)] @@ -14,6 +14,7 @@ implement_responder_for!(NewPostTemplate); fn new(_conn: utils::DbConn) -> utils::Page { utils::Page { title: String::from("New post"), + flash: None, content: NewPostTemplate { post: Default::default() } @@ -21,9 +22,9 @@ fn new(_conn: utils::DbConn) -> utils::Page { } #[post("/create", data="")] -fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Redirect { +fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Flash { ::models::Post::create(post.get(), conn); - Redirect::to("/") + Flash::success(Redirect::to("/"), "Post successfully created!") } #[derive(BartDisplay)] @@ -39,6 +40,7 @@ fn show(id: i32, conn: utils::DbConn) -> utils::Page { let p = ::models::Post::get(id, conn); utils::Page { title: p.title.clone(), + flash: None, content: ShowPostTemplate { post: p }, @@ -58,18 +60,19 @@ fn edit(id: i32, conn: utils::DbConn) -> utils::Page { let p = ::models::Post::get(id, conn); utils::Page { title: String::from("Edit post"), + flash: None, content: EditPostTemplate { post: p } } } #[post("/update", data="")] -fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Redirect { +fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Flash { ::models::Post::update(post.get(), conn); - Redirect::to("/") + Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("//delete", format = "text/html")] -fn delete(id: i32, conn: utils::DbConn) -> Redirect { +fn delete(id: i32, conn: utils::DbConn) -> Flash { ::models::Post::delete(id, conn); - Redirect::to("/") + Flash::success(Redirect::to("/"), "Post deleted!") } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2e79934..495440a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -49,6 +49,7 @@ impl Deref for DbConn { #[template = "templates/layout.html"] pub struct Page { pub title: String, + pub flash: Option, pub content: T } diff --git a/templates/layout.html b/templates/layout.html index b9c2046..caf41de 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -8,6 +8,7 @@ + {{#flash}}
{{ . }}
{{/flash}} {{{ content }}} -- cgit v1.2.3