Skip to content

Commit

Permalink
Create comments for an article
Browse files Browse the repository at this point in the history
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
msducheminjr committed Dec 29, 2021
1 parent c20c9a9 commit 6a8cd75
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
12 changes: 12 additions & 0 deletions app/controllers/comments_controller.rb
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
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
29 changes: 29 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,32 @@
<li><%= link_to "Destroy", article_path(@article),
data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %></li>
</ul>


<h2>Comments</h2>
<% @article.comments.each do |comment| %>
<p class="commenter">
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>

<p class="comment-body">
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>

<h2>Add a comment:</h2>
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
20 changes: 13 additions & 7 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ def setup
test "should get index" do
get articles_url
assert_response :success
assert_select 'h1', 'Articles'
assert_select 'li', Article.count
assert_select 'a', 'New Article'
assert_select "h1", "Articles"
assert_select "li", Article.count
assert_select "a", "New Article"
end

test "should get show" do
get article_url(@article)
assert_response :success
assert_select 'h1', @article.title
assert_select 'p', @article.body
assert_select 'a', 'Edit'
assert_select 'a', 'Destroy'
assert_select "h1", @article.title
assert_select "p", @article.body
assert_select "a", "Edit"
assert_select "a", "Destroy"
assert_select "h2", "Comments"
assert_select "p.commenter", @article.comments.count
assert_select "p.comment-body", @article.comments.count
assert_select "h2", "Add a comment:"
assert_select "form"
assert_select "form p", 3
end

test "should get new" do
Expand Down
27 changes: 27 additions & 0 deletions test/controllers/comments_controller_test.rb
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

0 comments on commit 6a8cd75

Please sign in to comment.