Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
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
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ApplicationRecord
belongs_to :post
end
7 changes: 7 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="<%= dom_id comment %>">
<p>
<strong>Post:</strong>
<%= comment.post_id %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/comments/_comment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! comment, :id, :post_id, :created_at, :updated_at
json.url comment_url(comment, format: :json)
22 changes: 22 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= form_with(model: comment) do |form| %>
<% if comment.errors.any? %>
<div style="color: red">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

<ul>
<% comment.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :post_id, style: "display: block" %>
<%= form.text_field :post_id %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
10 changes: 10 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing comment</h1>

<%= render "form", comment: @comment %>

<br>

<div>
<%= link_to "Show this comment", @comment %> |
<%= link_to "Back to comments", comments_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Comments</h1>

<div id="comments">
<% @comments.each do |comment| %>
<%= render comment %>
<p>
<%= link_to "Show this comment", comment %>
</p>
<% end %>
</div>

<%= link_to "New comment", new_comment_path %>
1 change: 1 addition & 0 deletions app/views/comments/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @comments, partial: "comments/comment", as: :comment
9 changes: 9 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New comment</h1>

<%= render "form", comment: @comment %>

<br>

<div>
<%= link_to "Back to comments", comments_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @comment %>

<div>
<%= link_to "Edit this comment", edit_comment_path(@comment) %> |
<%= link_to "Back to comments", comments_path %>

<%= button_to "Destroy this comment", @comment, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/comments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "comments/comment", comment: @comment
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :comments
root "hello#index"
end
9 changes: 9 additions & 0 deletions db/migrate/20240523083551_create_comments.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
post: one

two:
post: two
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
41 changes: 41 additions & 0 deletions test/system/comments_test.rb
Original file line number Diff line number Diff line change
@@ -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