Created
December 24, 2013 07:52
-
-
Save guinslym/8110087 to your computer and use it in GitHub Desktop.
Comments_controller. Books (Beginning Rails 4 (chapter 9)
This file contains hidden or 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_filter :load_article, :except => :destroy | |
before_filter :authenticate, :only => :destroy | |
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 | |
@article = current_user.articles.find(params[:article_id]) | |
@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