Created
May 4, 2020 08:55
-
-
Save 3h5a9/9444f8cc4387a81392d5f607c37677a8 to your computer and use it in GitHub Desktop.
Nested Comment wrong redirection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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