|
| 1 | +# This is the controller for blog posts. |
| 2 | + |
| 3 | + |
| 4 | +# We can access the blog at /blog |
1 | 5 | get "/blog" do |
| 6 | + #first we find all posts |
2 | 7 | @posts = Post.all |
| 8 | + |
| 9 | + #and then we render our template |
3 | 10 | haml :'posts/index' |
4 | 11 | end |
5 | 12 |
|
| 13 | +#Admin users can make new posts at /posts/new |
6 | 14 | get "/posts/new" do |
7 | | - unless logged_in? && current_user.admin? |
8 | | - flash[:error] = "Sorry, buddy" |
9 | | - redirect "/posts" |
10 | | - end |
| 15 | + #make sure we only let in admins! |
| 16 | + admin_only! |
11 | 17 |
|
| 18 | + #render the template |
12 | 19 | haml :'posts/new' |
13 | 20 | end |
14 | 21 |
|
| 22 | +#When a new post is made, it sends a POST request to /posts |
15 | 23 | post "/posts" do |
16 | | - unless logged_in? && current_user.admin? |
17 | | - flash[:error] = "Sorry, buddy" |
18 | | - redirect "/posts" |
19 | | - end |
| 24 | + #make sure we only let in admins! |
| 25 | + admin_only! |
20 | 26 |
|
| 27 | + #make the post with the given params |
21 | 28 | @post = Post.create(params) |
| 29 | + |
| 30 | + #set a friendly message |
22 | 31 | flash[:notice] = "Post Created" |
| 32 | + |
| 33 | + #go check out that post |
23 | 34 | redirect "/posts/#{@post.id}" |
24 | 35 | end |
25 | 36 |
|
| 37 | +#for completeness, we can also see every post at /posts |
26 | 38 | get "/posts" do |
| 39 | + #grab all of the posts |
27 | 40 | @posts = Post.all |
| 41 | + |
| 42 | + #render the template |
28 | 43 | haml :'posts/index' |
29 | 44 | end |
30 | 45 |
|
| 46 | +#an individual post can be seen at /posts/:id |
31 | 47 | get "/posts/:id" do |
| 48 | + #find the post with that id |
32 | 49 | @post = Post.find(params[:id]) |
| 50 | + |
| 51 | + #render our template |
33 | 52 | haml :'posts/show' |
34 | 53 | end |
35 | 54 |
|
| 55 | +#admins can edit posts at /posts/:id/edit |
36 | 56 | get "/posts/:id/edit" do |
37 | | - unless logged_in? && current_user.admin? |
38 | | - flash[:error] = "Sorry, buddy" |
39 | | - redirect "/posts" |
40 | | - end |
| 57 | + #make sure we only let in admins! |
| 58 | + admin_only! |
41 | 59 |
|
| 60 | + #find the post with the right id |
42 | 61 | @post = Post.find(params[:id]) |
| 62 | + |
| 63 | + #render our template |
43 | 64 | haml :'posts/edit' |
44 | 65 | end |
45 | 66 |
|
| 67 | +#to update a post, send a PUT request to /posts/:id |
46 | 68 | put "/posts/:id" do |
47 | | - unless logged_in? && current_user.admin? |
48 | | - flash[:error] = "Sorry, buddy" |
49 | | - redirect "/posts" |
50 | | - end |
| 69 | + #make sure we only let in admins! |
| 70 | + admin_only! |
| 71 | + |
| 72 | + #find the post with the right id |
51 | 73 | @post = Post.find(params[:id]) |
| 74 | + |
| 75 | + #update its info |
52 | 76 | @post.update_attributes(params) |
| 77 | + |
| 78 | + #set a friendly message |
53 | 79 | flash[:notice] = "Post Modified" |
| 80 | + |
| 81 | + #and visit that post |
54 | 82 | redirect "/posts/#{@post.id}" |
55 | 83 | end |
56 | 84 |
|
| 85 | +#I've included comments in here too, because comments can only be made on posts |
| 86 | +#make a new comment by sending a POST request to /comments |
57 | 87 | post "/comments" do |
58 | | - unless current_user |
59 | | - flash[:error] = "You must be logged in to comment!" |
60 | | - redirect "/posts" |
61 | | - end |
| 88 | + #we need to be logged in to comment |
| 89 | + #if we fail, return to /posts |
| 90 | + require_login! :return => "/posts" |
62 | 91 |
|
| 92 | + #set the email of the comment to our email |
63 | 93 | params[:comment]['user_email'] = current_user.email |
| 94 | + |
| 95 | + #find the post we want to comment on |
64 | 96 | @post = Post.find(params[:post_id]) |
| 97 | + |
| 98 | + # create our new comment and add it to the posts' comments |
65 | 99 | @post.comments << Comment.new(params[:comment]) |
66 | 100 | @post.save |
| 101 | + |
| 102 | + #set a helpful message |
67 | 103 | flash[:notice] = "Thanks for your comment!" |
| 104 | + |
| 105 | + #go back to the page for that post |
68 | 106 | redirect "/posts/#{@post.id}" |
69 | 107 | end |
0 commit comments