Guest User

Untitled

a guest
Jan 8th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. app/services/add_comment_to_post.rb
  2.  
  3. class AddCommentToPost
  4. def initialize(post:, comment:)
  5. @post = post
  6. @comment = comment
  7. end
  8.  
  9. def call
  10. @post.comments.create(@comment)
  11. @post
  12. end
  13. end
  14.  
  15.  
  16. app/services/remove_comment_from_class.rb
  17.  
  18. class RemoveCommentFromPost
  19. def initialize(post:, comment:)
  20. @post = post
  21. @comment = comment
  22. end
  23.  
  24. def call
  25. @post.comments.@comment.id.destroy
  26. @post
  27. end
  28. end
  29.  
  30. def create
  31. #this one works:
  32. #@post.comments.create! comment_params
  33.  
  34. AddCommentToPost.new(@post, @comment).call
  35. redirect_to @post
  36.  
  37.  
  38. def destroy
  39. RemoveCommentFromPost.new(@post,@comment).call
  40. redirect_to @post
  41.  
  42. Rails.application.routes.draw do
  43.  
  44. resources :posts do
  45. resources :comments
  46. end
  47.  
  48. root "posts#index"
  49.  
  50. end
Add Comment
Please, Sign In to add comment