Add comments'

This commit is contained in:
Ryan W
2021-12-16 14:33:08 -08:00
parent 4edabe6ab1
commit 3a146f7f60
15 changed files with 97 additions and 5 deletions
+13
View File
@@ -0,0 +1,13 @@
class CommentsController < ApplicationController
before_action :set_article
def create
@article.comments.create! params.required(:comment).permit(:content)
redirect_to @article
end
private
def set_article
@article = Article.find(params[:article_id])
end
end
+2
View File
@@ -0,0 +1,2 @@
module CommentsHelper
end
+1 -1
View File
@@ -1,5 +1,5 @@
class Article < ApplicationRecord
has_rich_text :text
has_many :comments, dependent: :destroy
has_rich_text :content
validates_presence_of :title
end
+4
View File
@@ -0,0 +1,4 @@
class Comment < ApplicationRecord
belongs_to :article
broadcasts_to :article
end
+10 -2
View File
@@ -5,8 +5,16 @@
</p>
<p>
<strong>Text:</strong>
<%= article.text %>
Posted <%= time_tag article.created_at %>
</p>
<p>
<strong><%= pluralize article.comments.count, "comment" %></strong>
</p>
<p>
<strong>Content:</strong>
<%= article.content %>
</p>
<p>
+7
View File
@@ -0,0 +1,7 @@
<h2>Comments</h2>
<div id="comments">
<%= render article.comments %>
</div>
<%= render "comments/new", article: article %>
+1
View File
@@ -3,6 +3,7 @@
<p id="notice"><%= notice %></p>
<%= render @article %>
<%= render "articles/comments", article: @article %>
<div>
<%= link_to "Edit this article", edit_article_path(@article) %> |
+4
View File
@@ -0,0 +1,4 @@
<div id="<%= dom_id comment %>">
<%= comment.content %>
- <%= time_tag comment.created_at %>
</div>
+5
View File
@@ -0,0 +1,5 @@
<%= form_with model: [ article, Comment.new ] do |form| %>
Your comment:<br>
<%= form.text_area :content, size: "20x5" %>
<%= form.submit %>
<% end %>