Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CommentsController < ApplicationController
- before_action :correct_user, only: :destroy
- def create
- @post =Post.find(params[:post_id])
- @comment =@post.comments.create(params[:comment].permit(:name, :body))
- redirect_to post_path(@post)
- end
- def destroy
- @post = Post.find(params[:post_id])
- @comment= @post.comments.find(params[:id])
- @comment.destroy
- redirect_to post_path(@post)
- end
- private
- def correct_user
- @user= User.find(current_user.id)
- redirect_to(root_url) unless current_user.id == @post.comment.user.id
- end
- end
- @post = Post.find(params[:post_id])
- @comment= @post.comments.find(params[:id])
- class Comment < ApplicationRecord
- belongs_to :post
- end
- class Post < ApplicationRecord
- belongs_to :user
- has_many :comments, dependent: :destroy
- validates :title, presence: true, length: {minimum: 5}
- validates :body, presence: true
- validates :user, presence: true
- validates :user_id, presence: true
- has_attached_file :image #, :styles => { :medium => "300x300>", :thumb =>
- "100x100>" }
- validates_attachment_content_type :image, :content_type => /Aimage/.*Z/
- end
- class User < ApplicationRecord
- has_many :posts
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- end
Add Comment
Please, Sign In to add comment