app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
def index
@comments = Comment.all
end
# GET /comments/1
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
def create
@comment = Comment.new(comment_params)
if @comment.save
redirect_to @comment.commentable
else
redirect_to :back, notice: @comment.errors.full_messages.join(" \n")
end
end
# PATCH/PUT /comments/1
def update
if @comment.update(comment_params)
respond_with @comment
else
render :edit
end
end
# DELETE /comments/1
def destroy
@comment.destroy
redirect_to comments_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def comment_params
params.require(:comment).permit(:user_id, :commentable_id, :commentable_type, :content)
end
end