Skip to content

Instantly share code, notes, and snippets.

/delete.diff Secret

Created March 14, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/38f67cf577beb228bbba to your computer and use it in GitHub Desktop.
Save anonymous/38f67cf577beb228bbba to your computer and use it in GitHub Desktop.
diff --git a/examples/blog/lib/Blog.pm b/examples/blog/lib/Blog.pm
index 2ce9f43..6ddf129 100644
--- a/examples/blog/lib/Blog.pm
+++ b/examples/blog/lib/Blog.pm
@@ -24,9 +24,10 @@ sub startup {
my $r = $self->routes;
$r->get('/' => sub { shift->redirect_to('posts') });
$r->get('/posts')->to('posts#index');
- $r->post('/posts')->to('posts#store')->name('store_post');
$r->get('/posts/create')->to('posts#create')->name('create_post');
+ $r->post('/posts')->to('posts#store')->name('store_post');
$r->get('/posts/:id')->to('posts#show')->name('show_post');
+ $r->delete('/posts/:id')->to('posts#remove')->name('remove_post');
}
1;
diff --git a/examples/blog/lib/Blog/Controller/Posts.pm b/examples/blog/lib/Blog/Controller/Posts.pm
index c6d98d5..4061234 100644
--- a/examples/blog/lib/Blog/Controller/Posts.pm
+++ b/examples/blog/lib/Blog/Controller/Posts.pm
@@ -1,6 +1,12 @@
package Blog::Controller::Posts;
use Mojo::Base 'Mojolicious::Controller';
+sub remove {
+ my $self = shift;
+ $self->posts->remove($self->param('id'));
+ $self->redirect_to('posts');
+}
+
sub show {
my $self = shift;
$self->stash(post => $self->posts->find($self->param('id')));
diff --git a/examples/blog/lib/Blog/Model/Posts.pm b/examples/blog/lib/Blog/Model/Posts.pm
index 94a5725..3f1c743 100644
--- a/examples/blog/lib/Blog/Model/Posts.pm
+++ b/examples/blog/lib/Blog/Model/Posts.pm
@@ -16,4 +16,6 @@ sub find {
return $self->pg->db->query('select * from posts where id = ?', $id)->hash;
}
+sub remove { shift->pg->db->query('delete from posts where id = ?', shift) }
+
1;
diff --git a/examples/blog/templates/posts/show.html.ep b/examples/blog/templates/posts/show.html.ep
index ddc112c..5a2fac0 100644
--- a/examples/blog/templates/posts/show.html.ep
+++ b/examples/blog/templates/posts/show.html.ep
@@ -1,3 +1,6 @@
% layout 'blog', title => $post->{title};
<h2><%= $post->{title} %></h2>
<p><%= $post->{body} %></p>
+%= form_for remove_post => {id => $post->{id}} => begin
+ %= submit_button 'Remove post'
+% end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment