Guest User

Untitled

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