Skip to content

Instantly share code, notes, and snippets.

@beliar91
Created December 21, 2015 10:37
Show Gist options
  • Save beliar91/ac66ae1da968ecc75057 to your computer and use it in GitHub Desktop.
Save beliar91/ac66ae1da968ecc75057 to your computer and use it in GitHub Desktop.
class CommentsController < ApplicationController
before_action :load_commentable, only: [:create, :index, :destroy]
before_action :find_comment, only: [:destroy]
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
redirect_to @commentable
end
end
def find_comment
@comment = Comment.find(params[:id])
end
def destroy
@comment.destroy
redirect_to @commentable
end
private
def load_commentable
resource, id = request.path.split('/')[1,2]
@commentable = resource.singularize.classify.constantize.find(id)
end
def comment_params
params.require(:comment).permit(:content, :commentable_type, :commentable_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment