Add comments'
This commit is contained in:
@@ -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
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
module CommentsHelper
|
||||||
|
end
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
class Article < ApplicationRecord
|
class Article < ApplicationRecord
|
||||||
has_rich_text :text
|
has_many :comments, dependent: :destroy
|
||||||
has_rich_text :content
|
has_rich_text :content
|
||||||
validates_presence_of :title
|
validates_presence_of :title
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
class Comment < ApplicationRecord
|
||||||
|
belongs_to :article
|
||||||
|
broadcasts_to :article
|
||||||
|
end
|
||||||
@@ -5,8 +5,16 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<strong>Text:</strong>
|
Posted <%= time_tag article.created_at %>
|
||||||
<%= article.text %>
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<strong><%= pluralize article.comments.count, "comment" %></strong>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<strong>Content:</strong>
|
||||||
|
<%= article.content %>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<h2>Comments</h2>
|
||||||
|
|
||||||
|
<div id="comments">
|
||||||
|
<%= render article.comments %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<%= render "comments/new", article: article %>
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
<p id="notice"><%= notice %></p>
|
<p id="notice"><%= notice %></p>
|
||||||
|
|
||||||
<%= render @article %>
|
<%= render @article %>
|
||||||
|
<%= render "articles/comments", article: @article %>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<%= link_to "Edit this article", edit_article_path(@article) %> |
|
<%= link_to "Edit this article", edit_article_path(@article) %> |
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<div id="<%= dom_id comment %>">
|
||||||
|
<%= comment.content %>
|
||||||
|
- <%= time_tag comment.created_at %>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<%= form_with model: [ article, Comment.new ] do |form| %>
|
||||||
|
Your comment:<br>
|
||||||
|
<%= form.text_area :content, size: "20x5" %>
|
||||||
|
<%= form.submit %>
|
||||||
|
<% end %>
|
||||||
+3
-1
@@ -3,7 +3,9 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
get 'welcome/index'
|
get 'welcome/index'
|
||||||
|
|
||||||
resources :articles
|
resources :articles do
|
||||||
|
resources :comments
|
||||||
|
end
|
||||||
|
|
||||||
root 'welcome#index'
|
root 'welcome#index'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
class CreateComments < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
create_table :comments do |t|
|
||||||
|
t.references :article, null: false, foreign_key: true
|
||||||
|
t.text :content
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
+10
-1
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_12_16_184103) do
|
ActiveRecord::Schema.define(version: 2021_12_16_185503) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -60,6 +60,15 @@ ActiveRecord::Schema.define(version: 2021_12_16_184103) do
|
|||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "comments", force: :cascade do |t|
|
||||||
|
t.bigint "article_id", null: false
|
||||||
|
t.text "content"
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.index ["article_id"], name: "index_comments_on_article_id"
|
||||||
|
end
|
||||||
|
|
||||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||||
|
add_foreign_key "comments", "articles"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
# Specs in this file have access to a helper object that includes
|
||||||
|
# the CommentsHelper. For example:
|
||||||
|
#
|
||||||
|
# describe CommentsHelper do
|
||||||
|
# describe "string concat" do
|
||||||
|
# it "concats two strings with spaces" do
|
||||||
|
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
RSpec.describe CommentsHelper, type: :helper do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Comment, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe "Comments", type: :request do
|
||||||
|
describe "GET /index" do
|
||||||
|
pending "add some examples (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user