Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class PostsController < ApplicationController
  2. def show
  3. @post = Post.find(params[:id])
  4. end
  5.  
  6. def new
  7. # Hannah remove please, add in next line: @topic = Topic.find(params[:id])
  8. @topic = Topic.find(params[:topic_id])
  9. @post = Post.new
  10. end
  11.  
  12. def create
  13. #create a new instance of post
  14. @post = Post.new
  15. @post.title = params[:post][:title]
  16. @post.body = params[:post][:body]
  17. # Hannah remove please. Add in next 2 lines: @topic = Topic.find(params[:id])
  18. # @post.topic = topic
  19. topic = Topic.find(params[:topic_id])
  20. @post.topic = topic
  21.  
  22. #if post is successfully saved, display success message
  23. if @post.save
  24. flash[:notice] = "Post was saved."
  25. # remove @topic and make it topic
  26. redirect_to [topic, @post]
  27. else
  28. #if save is unsuccessful, display error message and render the new view again
  29. flash.now[:alert] = "There was an error saving the post. Please try again."
  30. render :new
  31. end
  32. end
  33.  
  34. def edit
  35. @post = Post.find(params[:id])
  36. end
  37.  
  38. def update
  39. @post = Post.find(params[:id])
  40. @post.title = params[:post][:title]
  41. @post.body = params[:post][:body]
  42.  
  43. if @post.save
  44. flash[:notice] = "Post was updated"
  45. # Hannah remove please and add in next line: redirect_to [@topic, @post]
  46. redirect_to [@post.topic, @post]
  47. else
  48. flash.now[:alert] = "There was an error updating. Please try again."
  49. render :edit
  50. end
  51. end
  52.  
  53. def destroy
  54. @post = Post.find(params[:id])
  55. if @post.destroy
  56. flash[:notice] = "\"#{@post.title}\" was deleted successfully."
  57. redirect_to @post.topic
  58. else
  59. flash.now[:alert] = "There was a problem deleting the post."
  60. render :show
  61. end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement