Guest User

Untitled

a guest
Dec 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class CommentsController < ApplicationController
  2. before_action :correct_user, only: :destroy
  3.  
  4. def create
  5. @post =Post.find(params[:post_id])
  6. @comment =@post.comments.create(params[:comment].permit(:name, :body))
  7. redirect_to post_path(@post)
  8. end
  9.  
  10. def destroy
  11. @post = Post.find(params[:post_id])
  12. @comment= @post.comments.find(params[:id])
  13. @comment.destroy
  14. redirect_to post_path(@post)
  15. end
  16.  
  17. private
  18. def correct_user
  19. @user= User.find(current_user.id)
  20. redirect_to(root_url) unless current_user.id == @post.comment.user.id
  21. end
  22.  
  23. end
  24.  
  25. @post = Post.find(params[:post_id])
  26. @comment= @post.comments.find(params[:id])
  27.  
  28. class Comment < ApplicationRecord
  29. belongs_to :post
  30. end
  31.  
  32. class Post < ApplicationRecord
  33. belongs_to :user
  34. has_many :comments, dependent: :destroy
  35.  
  36. validates :title, presence: true, length: {minimum: 5}
  37. validates :body, presence: true
  38. validates :user, presence: true
  39. validates :user_id, presence: true
  40. has_attached_file :image #, :styles => { :medium => "300x300>", :thumb =>
  41. "100x100>" }
  42. validates_attachment_content_type :image, :content_type => /Aimage/.*Z/
  43. end
  44.  
  45. class User < ApplicationRecord
  46. has_many :posts
  47.  
  48. devise :database_authenticatable, :registerable,
  49. :recoverable, :rememberable, :trackable, :validatable
  50. end
Add Comment
Please, Sign In to add comment