|
31 | 31 | flash[:notice] = "Post Created" |
32 | 32 |
|
33 | 33 | #go check out that post |
34 | | - redirect "/posts/#{@post.id}" |
| 34 | + redirect "/posts/#{@post.slug}" |
35 | 35 | end |
36 | 36 |
|
37 | 37 | #for completeness, we can also see every post at /posts |
|
43 | 43 | haml :'posts/index' |
44 | 44 | end |
45 | 45 |
|
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]) |
50 | 50 |
|
51 | 51 | #render our template |
52 | 52 | haml :'posts/show' |
53 | 53 | end |
54 | 54 |
|
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 |
57 | 57 | #make sure we only let in admins! |
58 | 58 | admin_only! |
59 | 59 |
|
60 | 60 | #find the post with the right id |
61 | | - @post = Post.find(params[:id]) |
| 61 | + @post = Post.first(:slug => params[:slug]) |
62 | 62 |
|
63 | 63 | #render our template |
64 | 64 | haml :'posts/edit' |
65 | 65 | end |
66 | 66 |
|
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 |
69 | 69 | #make sure we only let in admins! |
70 | 70 | admin_only! |
71 | 71 |
|
72 | 72 | #find the post with the right id |
73 | | - @post = Post.find(params[:id]) |
| 73 | + @post = Post.first(:slug => params[:slug]) |
74 | 74 |
|
75 | 75 | #update its info |
76 | 76 | @post.update_attributes(params) |
|
79 | 79 | flash[:notice] = "Post Modified" |
80 | 80 |
|
81 | 81 | #and visit that post |
82 | | - redirect "/posts/#{@post.id}" |
| 82 | + redirect "/posts/#{@post.slug}" |
83 | 83 | end |
84 | 84 |
|
85 | 85 | #I've included comments in here too, because comments can only be made on posts |
|
93 | 93 | params[:comment]['author'] = current_user.username |
94 | 94 |
|
95 | 95 | #find the post we want to comment on |
96 | | - @post = Post.find(params[:post_id]) |
| 96 | + @post = Post.first(:slug => params[:post_slug]) |
97 | 97 |
|
98 | 98 | # create our new comment and add it to the posts' comments |
99 | 99 | @post.comments << Comment.new(params[:comment]) |
|
103 | 103 | flash[:notice] = "Thanks for your comment!" |
104 | 104 |
|
105 | 105 | #go back to the page for that post |
106 | | - redirect "/posts/#{@post.id}" |
| 106 | + redirect "/posts/#{@post.slug}" |
107 | 107 | end |
0 commit comments