-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CommentsControler (with only a create action as-of now) and the associated test class. Modify the articles show view to provide a list of comments for the article and a form for adding a new comment to the article. Modify the ArticlesControllerTest with assertions about the new functionality for the show action and view. Completes Rails Getting Started Guide: * 8 Adding a Second Model * 8.4 Generating a Controller
- Loading branch information
1 parent
c20c9a9
commit 6a8cd75
Showing
5 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CommentsController < ApplicationController | ||
def create | ||
@article = Article.find(params[:article_id]) | ||
@comment = @article.comments.create(comment_params) | ||
redirect_to article_path(@article), notice: "Comment was successfully created." | ||
end | ||
|
||
private | ||
def comment_params | ||
params.require(:comment).permit(:commenter, :body) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module CommentsHelper | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "test_helper" | ||
|
||
class CommentsControllerTest < ActionDispatch::IntegrationTest | ||
def setup | ||
@article = articles(:nerd) | ||
end | ||
|
||
test "should create comment" do | ||
commenter = "A new commenter" | ||
body = "This is my comment on the Nerd Dice article." | ||
assert_difference("Comment.count") do | ||
post article_comments_url(@article), params: { | ||
article_id: @article.id, | ||
comment: { | ||
commenter: commenter, | ||
body: body | ||
} | ||
} | ||
end | ||
|
||
comment = @article.comments.last | ||
assert_redirected_to article_path(@article) | ||
assert_equal commenter, comment.commenter | ||
assert_equal body, comment.body | ||
assert_equal "Comment was successfully created.", flash[:notice] | ||
end | ||
end |