Skip to content

Instantly share code, notes, and snippets.

@ccjr
Created April 3, 2010 19:57
Show Gist options
  • Save ccjr/354809 to your computer and use it in GitHub Desktop.
Save ccjr/354809 to your computer and use it in GitHub Desktop.
[Beginning Rails 3] Listing 8-11. The comments controllert in app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_filter :load_article, :except => :destroy
before_filter :authenticate, :only => :destroy
def create
@comment = @article.comments.new(params[:comment])
if @comment.save
respond_to do |format|
format.html { redirect_to @article, :notice => 'Thanks for your comment' }
format.js
end
else
respond_to do |format|
format.html { redirect_to @article, :alert => 'Unable to add comment' }
format.js { render 'fail_create.js.erb' }
end
end
end
def destroy
@article = current_user.articles.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to @article, :notice => 'Comment deleted' }
format.js
end
end
private
def load_article
@article = Article.find(params[:article_id])
end
end
@guinslym
Copy link

In the private section there is a missing function:
def comment_params
params.require(:comment).permit(:name, :email, :body)
end
which is in the book ... but not here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment