diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..e0cd3d6
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,70 @@
+class CommentsController < ApplicationController
+ before_action :set_comment, only: %i[ show edit update destroy ]
+
+ # GET /comments or /comments.json
+ def index
+ @comments = Comment.alll
+ end
+
+ # GET /comments/1 or /comments/1.json
+ def show
+ end
+
+ # GET /comments/new
+ def new
+ @comment = Comment.new
+ end
+
+ # GET /comments/1/edit
+ def edit
+ end
+
+ # POST /comments or /comments.json
+ def create
+ @comment = Comment.new(comment_params)
+
+ respond_to do |format|
+ if @comment.save
+ format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." }
+ format.json { render :show, status: :created, location: @comment }
+ else
+ format.html { render :new, status: :unprocessable_entity }
+ format.json { render json: @comment.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PATCH/PUT /comments/1 or /comments/1.json
+ def update
+ respond_to do |format|
+ if @comment.update(comment_params)
+ format.html { redirect_to comment_url(@comment), notice: "Comment was successfully updated." }
+ format.json { render :show, status: :ok, location: @comment }
+ else
+ format.html { render :edit, status: :unprocessable_entity }
+ format.json { render json: @comment.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /comments/1 or /comments/1.json
+ def destroy
+ @comment.destroy!
+
+ respond_to do |format|
+ format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_comment
+ @comment = Comment.find(params[:id])
+ end
+
+ # Only allow a list of trusted parameters through.
+ def comment_params
+ params.require(:comment).permit(:post_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/comment.rb b/app/models/comment.rb
new file mode 100644
index 0000000..8b86c56
--- /dev/null
+++ b/app/models/comment.rb
@@ -0,0 +1,3 @@
+class Comment < ApplicationRecord
+ belongs_to :post
+end
diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb
new file mode 100644
index 0000000..44bf97e
--- /dev/null
+++ b/app/views/comments/_comment.html.erb
@@ -0,0 +1,7 @@
+
diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder
new file mode 100644
index 0000000..d90476f
--- /dev/null
+++ b/app/views/comments/_comment.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! comment, :id, :post_id, :created_at, :updated_at
+json.url comment_url(comment, format: :json)
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb
new file mode 100644
index 0000000..fd450b0
--- /dev/null
+++ b/app/views/comments/_form.html.erb
@@ -0,0 +1,22 @@
+<%= form_with(model: comment) do |form| %>
+ <% if comment.errors.any? %>
+
+
<%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:
+
+
+ <% comment.errors.each do |error| %>
+ - <%= error.full_message %>
+ <% end %>
+
+
+ <% end %>
+
+
+ <%= form.label :post_id, style: "display: block" %>
+ <%= form.text_field :post_id %>
+
+
+
+ <%= form.submit %>
+
+<% end %>
diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb
new file mode 100644
index 0000000..9720435
--- /dev/null
+++ b/app/views/comments/edit.html.erb
@@ -0,0 +1,10 @@
+Editing comment
+
+<%= render "form", comment: @comment %>
+
+
+
+
+ <%= link_to "Show this comment", @comment %> |
+ <%= link_to "Back to comments", comments_path %>
+
diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb
new file mode 100644
index 0000000..c493515
--- /dev/null
+++ b/app/views/comments/index.html.erb
@@ -0,0 +1,14 @@
+<%= notice %>
+
+Comments
+
+
+
+<%= link_to "New comment", new_comment_path %>
diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder
new file mode 100644
index 0000000..e3322af
--- /dev/null
+++ b/app/views/comments/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @comments, partial: "comments/comment", as: :comment
diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb
new file mode 100644
index 0000000..6ba6dd8
--- /dev/null
+++ b/app/views/comments/new.html.erb
@@ -0,0 +1,9 @@
+New comment
+
+<%= render "form", comment: @comment %>
+
+
+
+
+ <%= link_to "Back to comments", comments_path %>
+
diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb
new file mode 100644
index 0000000..b90af0a
--- /dev/null
+++ b/app/views/comments/show.html.erb
@@ -0,0 +1,10 @@
+<%= notice %>
+
+<%= render @comment %>
+
+
+ <%= link_to "Edit this comment", edit_comment_path(@comment) %> |
+ <%= link_to "Back to comments", comments_path %>
+
+ <%= button_to "Destroy this comment", @comment, method: :delete %>
+
diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder
new file mode 100644
index 0000000..78a9099
--- /dev/null
+++ b/app/views/comments/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "comments/comment", comment: @comment
diff --git a/config/routes.rb b/config/routes.rb
index f28ca9f..4242355 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
+ resources :comments
root "hello#index"
end
diff --git a/db/migrate/20240523083551_create_comments.rb b/db/migrate/20240523083551_create_comments.rb
new file mode 100644
index 0000000..8e0521d
--- /dev/null
+++ b/db/migrate/20240523083551_create_comments.rb
@@ -0,0 +1,9 @@
+class CreateComments < ActiveRecord::Migration[7.1]
+ def change
+ create_table :comments do |t|
+ t.references :post, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb
new file mode 100644
index 0000000..0002954
--- /dev/null
+++ b/test/controllers/comments_controller_test.rb
@@ -0,0 +1,48 @@
+require "test_helper"
+
+class CommentsControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ @comment = comments(:one)
+ end
+
+ test "should get index" do
+ get comments_url
+ assert_response :success
+ end
+
+ test "should get new" do
+ get new_comment_url
+ assert_response :success
+ end
+
+ test "should create comment" do
+ assert_difference("Comment.count") do
+ post comments_url, params: { comment: { post_id: @comment.post_id } }
+ end
+
+ assert_redirected_to comment_url(Comment.last)
+ end
+
+ test "should show comment" do
+ get comment_url(@comment)
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get edit_comment_url(@comment)
+ assert_response :success
+ end
+
+ test "should update comment" do
+ patch comment_url(@comment), params: { comment: { post_id: @comment.post_id } }
+ assert_redirected_to comment_url(@comment)
+ end
+
+ test "should destroy comment" do
+ assert_difference("Comment.count", -1) do
+ delete comment_url(@comment)
+ end
+
+ assert_redirected_to comments_url
+ end
+end
diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml
new file mode 100644
index 0000000..227ffb0
--- /dev/null
+++ b/test/fixtures/comments.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ post: one
+
+two:
+ post: two
diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb
new file mode 100644
index 0000000..5a6feda
--- /dev/null
+++ b/test/models/comment_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/system/comments_test.rb b/test/system/comments_test.rb
new file mode 100644
index 0000000..cf8a961
--- /dev/null
+++ b/test/system/comments_test.rb
@@ -0,0 +1,41 @@
+require "application_system_test_case"
+
+class CommentsTest < ApplicationSystemTestCase
+ setup do
+ @comment = comments(:one)
+ end
+
+ test "visiting the index" do
+ visit comments_url
+ assert_selector "h1", text: "Comments"
+ end
+
+ test "should create comment" do
+ visit comments_url
+ click_on "New comment"
+
+ fill_in "Post", with: @comment.post_id
+ click_on "Create Comment"
+
+ assert_text "Comment was successfully created"
+ click_on "Back"
+ end
+
+ test "should update Comment" do
+ visit comment_url(@comment)
+ click_on "Edit this comment", match: :first
+
+ fill_in "Post", with: @comment.post_id
+ click_on "Update Comment"
+
+ assert_text "Comment was successfully updated"
+ click_on "Back"
+ end
+
+ test "should destroy Comment" do
+ visit comment_url(@comment)
+ click_on "Destroy this comment", match: :first
+
+ assert_text "Comment was successfully destroyed"
+ end
+end
+ <%= link_to "Show this comment", comment %> +
+ <% end %> +