diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-01-14 23:21:10 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-01-14 23:21:10 +0100 |
commit | bf2e515855ffa04c8884a14c872001dd378d673e (patch) | |
tree | d515bdac9671e7675453b3d7b157b5a521fff21f | |
parent | d4d23a484dac966e83b625edcaa867c0f3137b5e (diff) | |
download | rocket-blog-bf2e515855ffa04c8884a14c872001dd378d673e.tar.gz rocket-blog-bf2e515855ffa04c8884a14c872001dd378d673e.tar.bz2 rocket-blog-bf2e515855ffa04c8884a14c872001dd378d673e.zip |
Add flash messages when adding/editing/deleting posts.
-rw-r--r-- | src/controllers/home_controller.rs | 3 | ||||
-rw-r--r-- | src/controllers/posts_controller.rs | 17 | ||||
-rw-r--r-- | src/utils/mod.rs | 1 | ||||
-rw-r--r-- | 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<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())), 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<NewPostTemplate> { 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<NewPostTemplate> { } #[post("/create", data="<post>")] -fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Redirect { +fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Flash<Redirect> { ::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<ShowPostTemplate> { 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<EditPostTemplate> { let p = ::models::Post::get(id, conn); utils::Page { title: String::from("Edit post"), + flash: None, content: EditPostTemplate { post: p } } } #[post("/update", data="<post>")] -fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Redirect { +fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Flash<Redirect> { ::models::Post::update(post.get(), conn); - Redirect::to("/") + Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("/<id>/delete", format = "text/html")] -fn delete(id: i32, conn: utils::DbConn) -> Redirect { +fn delete(id: i32, conn: utils::DbConn) -> Flash<Redirect> { ::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<T: Display> { pub title: String, + pub flash: Option<String>, 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 @@ <header class="page-header"> <h1>{{ title }}</title> </header> + {{#flash}}<div class="flash">{{ . }}</div>{{/flash}} {{{ content }}} </body> </html> |