Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
babanesma committed Mar 16, 2023
1 parent 2724599 commit 6d7e76e
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CommentsController < ApplicationController
before_action :set_post

def create
@post.comments.create! params.required(:comment).permit(:content)
redirect_to @post
end

private
def set_post
@post = Post.find(params[: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
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Post < ApplicationRecord
validates_presence_of :title
has_rich_text :content
has_many :comments
end
4 changes: 4 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div id="<%= dom_id(comment) %>">
<%= comment.content %>
- <%= time_tag comment.created_at, "data-local": "time-ago" %>
</div>
5 changes: 5 additions & 0 deletions app/views/comments/_new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= form_with model: [ post, Comment.new ] do |form| %>
Your comment: <br>
<%= form.text_area :content, size: "anoth20x5" %>
<%= form.submit %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/posts/_comments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2>Comments</h2>

<div id="comments">
<%= render post.comments %>
</div>

<%= render "comments/new", post: post %>
2 changes: 1 addition & 1 deletion app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</p>

<p>Posted <%= time_tag post.created_at, "data-local": "time-ago" %></p>

<p><strong><%= pluralize(post.comments.count, "comment") %></strong></p>
<p>
<strong>Content:</strong>
<%= post.content %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p style="color: green"><%= notice %></p>

<%= render @post %>

<%= render "posts/comments", post: @post%>
<div>
<%= link_to "Edit this post", edit_post_path(@post) %> |
<%= link_to "Back to posts", posts_path %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
resources :posts
resources :posts do
resources :comments
end
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20230316193338_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateComments < ActiveRecord::Migration[7.0]
def change
create_table :comments do |t|
t.references :post, null: false, foreign_key: true
t.text :content

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

class CommentsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
9 changes: 9 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
post: one
content: MyText

two:
post: two
content: MyText
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

0 comments on commit 6d7e76e

Please sign in to comment.