Skip to content

Commit 11306bc

Browse files
committed
posts now have a slug
1 parent ebddb0c commit 11306bc

5 files changed

Lines changed: 24 additions & 15 deletions

File tree

controllers/posts.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
flash[:notice] = "Post Created"
3232

3333
#go check out that post
34-
redirect "/posts/#{@post.id}"
34+
redirect "/posts/#{@post.slug}"
3535
end
3636

3737
#for completeness, we can also see every post at /posts
@@ -43,34 +43,34 @@
4343
haml :'posts/index'
4444
end
4545

46-
#an individual post can be seen at /posts/:id
47-
get "/posts/:id" do
48-
#find the post with that id
49-
@post = Post.find(params[:id])
46+
#an individual post can be seen at /posts/:slug
47+
get "/posts/:slug" do
48+
#find the post with that slug
49+
@post = Post.first(:slug => params[:slug])
5050

5151
#render our template
5252
haml :'posts/show'
5353
end
5454

55-
#admins can edit posts at /posts/:id/edit
56-
get "/posts/:id/edit" do
55+
#admins can edit posts at /posts/:slug/edit
56+
get "/posts/:slug/edit" do
5757
#make sure we only let in admins!
5858
admin_only!
5959

6060
#find the post with the right id
61-
@post = Post.find(params[:id])
61+
@post = Post.first(:slug => params[:slug])
6262

6363
#render our template
6464
haml :'posts/edit'
6565
end
6666

67-
#to update a post, send a PUT request to /posts/:id
68-
put "/posts/:id" do
67+
#to update a post, send a PUT request to /posts/:slug
68+
put "/posts/:slug" do
6969
#make sure we only let in admins!
7070
admin_only!
7171

7272
#find the post with the right id
73-
@post = Post.find(params[:id])
73+
@post = Post.first(:slug => params[:slug])
7474

7575
#update its info
7676
@post.update_attributes(params)
@@ -79,7 +79,7 @@
7979
flash[:notice] = "Post Modified"
8080

8181
#and visit that post
82-
redirect "/posts/#{@post.id}"
82+
redirect "/posts/#{@post.slug}"
8383
end
8484

8585
#I've included comments in here too, because comments can only be made on posts
@@ -93,7 +93,7 @@
9393
params[:comment]['author'] = current_user.username
9494

9595
#find the post we want to comment on
96-
@post = Post.find(params[:post_id])
96+
@post = Post.first(:slug => params[:post_slug])
9797

9898
# create our new comment and add it to the posts' comments
9999
@post.comments << Comment.new(params[:comment])
@@ -103,5 +103,5 @@
103103
flash[:notice] = "Thanks for your comment!"
104104

105105
#go back to the page for that post
106-
redirect "/posts/#{@post.id}"
106+
redirect "/posts/#{@post.slug}"
107107
end

models/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Post
66
key :title, String
77
key :body, String
88

9+
key :slug, String
10+
911
many :comments
1012
timestamps!
1113
end

views/posts/edit.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
%label{:for => 'post_title'} Title
44
%br
55
%input{ :type => 'text', :name => 'title', :id => 'post_title', :value => @post.title}
6+
%label{:for => 'post_slug'} Title
7+
%br
8+
%input{ :type => 'text', :name => 'slug', :id => 'post_slug', :value => @post.slug}
69
%br
710
%label{:for => 'post_body'} Body
811
%br

views/posts/new.haml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
%br
44
%input{ :type => 'text', :name => 'title', :id => 'post_title'}
55
%br
6+
%label{:for => 'post_slug'} Title
7+
%br
8+
%input{ :type => 'text', :name => 'slug', :id => 'post_slug'}
9+
%br
610
%label{:for => 'post_body'} Body
711
%br
812
%textarea{ :rows => '10', :cols => '30', :name => 'body', :id => 'post_body' }

views/posts/show.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- if logged_in?
1919
%hr
2020
%form{:action => "/comments", :method => "POST"}
21-
%input{:type => "hidden", :id => "post_id", :name => "post_id", :value => @post.id}
21+
%input{:type => "hidden", :id => "post_slug", :name => "post_slug", :value => @post.slug}
2222
%label{:for => "comment_body"} Submit your opinion:
2323
%br
2424
%textarea{:id => "comment_body", :name => "comment[body]", :cols => 70, :rows => 5}

0 commit comments

Comments
 (0)