Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nested Comment wrong redirection.
class CommentsController < ApplicationController
before_action :find_commentable, only: %i[new create destroy]
def new
@comment = Comment.new
end
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
flash[:success] = 'Comment successfully created'
redirect_to @commentable
else
flash[:error] = 'Something went wrong'
render 'new'
end
end
def destroy
@comment = @commentable.comments.find(params[:id])
if @comment.destroy
redirect_to @comment.commentable
flash[:success] = 'Object was successfully deleted.'
else
redirect_to posts_path(@post)
flash[:error] = 'Something went wrong'
end
end
private
def comment_params
params.require(:comment).permit(:content)
end
def find_commentable
@commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment