Skip to content

Instantly share code, notes, and snippets.

@nicedawg
Created February 22, 2020 23:28
Show Gist options
  • Save nicedawg/3c7aa2c1f8c4c4e37f8398360989b06f to your computer and use it in GitHub Desktop.
Save nicedawg/3c7aa2c1f8c4c4e37f8398360989b06f to your computer and use it in GitHub Desktop.
[Beginning Rails 6] Listing 8-8. Comments Controller in app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :load_article
def create
@comment = @article.comments.new(comment_params)
if @comment.save
redirect_to @article, notice: 'Thanks for your comment'
else
redirect_to @article, alert: 'Unable to add comment'
end
end
def destroy
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to @article, notice: 'Comment deleted'
end
private
def load_article
@article = Article.find(params[:article_id])
end
def comment_params
params.require(:comment).permit(:name, :email, :body)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment