diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..ad1568a --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/article.rb b/app/models/article.rb index 573f062..f245902 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..48e21c4 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ApplicationRecord + belongs_to :article + broadcasts_to :article +end diff --git a/app/views/articles/_article.html.erb b/app/views/articles/_article.html.erb index 3c37f1a..d3dda2c 100644 --- a/app/views/articles/_article.html.erb +++ b/app/views/articles/_article.html.erb @@ -5,8 +5,16 @@

- Text: - <%= article.text %> + Posted <%= time_tag article.created_at %> +

+ +

+ <%= pluralize article.comments.count, "comment" %> +

+ +

+ Content: + <%= article.content %>

diff --git a/app/views/articles/_comments.html.erb b/app/views/articles/_comments.html.erb new file mode 100644 index 0000000..cd7329d --- /dev/null +++ b/app/views/articles/_comments.html.erb @@ -0,0 +1,7 @@ +

Comments

+ +
+ <%= render article.comments %> +
+ +<%= render "comments/new", article: article %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 8edacbc..5c96404 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -3,6 +3,7 @@

<%= notice %>

<%= render @article %> +<%= render "articles/comments", article: @article %>
<%= link_to "Edit this article", edit_article_path(@article) %> | diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb new file mode 100644 index 0000000..216a9c2 --- /dev/null +++ b/app/views/comments/_comment.html.erb @@ -0,0 +1,4 @@ +
+ <%= comment.content %> + - <%= time_tag comment.created_at %> +
diff --git a/app/views/comments/_new.html.erb b/app/views/comments/_new.html.erb new file mode 100644 index 0000000..3e42623 --- /dev/null +++ b/app/views/comments/_new.html.erb @@ -0,0 +1,5 @@ +<%= form_with model: [ article, Comment.new ] do |form| %> + Your comment:
+ <%= form.text_area :content, size: "20x5" %> + <%= form.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 13e9914..79a5694 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,9 @@ Rails.application.routes.draw do get 'welcome/index' - resources :articles + resources :articles do + resources :comments + end root 'welcome#index' end diff --git a/db/migrate/20211216185503_create_comments.rb b/db/migrate/20211216185503_create_comments.rb new file mode 100644 index 0000000..0a9772a --- /dev/null +++ b/db/migrate/20211216185503_create_comments.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b34eced..1ea3384 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 enable_extension "plpgsql" @@ -60,6 +60,15 @@ ActiveRecord::Schema.define(version: 2021_12_16_184103) do t.datetime "updated_at", precision: 6, null: false 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_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "comments", "articles" end diff --git a/spec/helpers/comments_helper_spec.rb b/spec/helpers/comments_helper_spec.rb new file mode 100644 index 0000000..729cd87 --- /dev/null +++ b/spec/helpers/comments_helper_spec.rb @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 0000000..c10688d --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Comment, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb new file mode 100644 index 0000000..3161a1c --- /dev/null +++ b/spec/requests/comments_spec.rb @@ -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