Skip to content

Commit 3ef6668

Browse files
author
github_username
committed
Initial commit
1 parent 3485b8d commit 3ef6668

14 files changed

+111
-5
lines changed

app/controllers/articles_controller.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ def update
3131
render :edit, status: :unprocessable_entity
3232
end
3333
end
34-
34+
35+
def destroy
36+
@article = Article.find(params[:id])
37+
@article.destroy
38+
39+
redirect_to root_path, status: see_other
40+
end
41+
3542
private
3643
def article_params
3744
params.require(:article).permit(:title, :body)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CommentsController < ApplicationController
2+
def create
3+
@article = Article.find(params[:article_id])
4+
@comment = @article.comments.create(comment.params)
5+
redirect_to article_path(@article)
6+
end
7+
private
8+
def comment_params
9+
params.require(:comment).permit(:commenter, :body)
10+
end
11+
end

app/helpers/comments_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module CommentsHelper
2+
end

app/models/article.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Article < ApplicationRecord
2+
has_many :comments
23
validates :title, presence: true
34
validates :body, presence: true, length: {minimum: 10 }
45
end

app/models/comment.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Comment < ApplicationRecord
2+
belongs_to :article
3+
end

app/views/articles/show.html.erb

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
<h1><%= @article.title %></h1>
22

33
<p><%= @article.body %></p>
4+
45
<ul>
5-
<li> <%= link_to "Edit", edit_article_path(@article) %></li>
6-
</ul>
6+
<li><%= link_to "Edit", edit_article_path(@article) %></li>
7+
<li><%= link_to "Destroy", article_path(@article), data: {
8+
turbo_method: :delete,
9+
turbo_confirm: "Are you sure?"
10+
} %></li>
11+
</ul>
12+
13+
<h2>Comments</h2>
14+
<%= render @article.comments %>
15+
16+
17+
<h2>Add a comment:</h2>
18+
<%= render 'comments/form' %>

app/views/comments/_form.html.erb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
2+
<p>
3+
<%= form.label :commenter %><br>
4+
<%= form.text_field :commenter %>
5+
</p>
6+
<p>
7+
<%= form.label :body %><br>
8+
<%= form.text_area :body %>
9+
</p>
10+
<p>
11+
<%= form.submit %>
12+
</p>
13+
<% end %>

app/views/comments/comment.html.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>
2+
<strong>Commenter:</strong>
3+
<%= comment.commenter %>
4+
</p>
5+
6+
<p>
7+
<strong>Comment:</strong>
8+
<%= comment.body %>
9+
</p>

config/routes.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Rails.application.routes.draw do
22
root "articles#index"
33

4-
resources :articles
4+
resources :articles do
5+
resources :comments
56
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateComments < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :comments do |t|
4+
t.string :commenter
5+
t.text :body
6+
t.references :article, null: false, foreign_key: true
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class CommentsControllerTest < ActionDispatch::IntegrationTest
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

test/fixtures/comments.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
commenter: MyString
5+
body: MyText
6+
article: one
7+
8+
two:
9+
commenter: MyString
10+
body: MyText
11+
article: two

test/models/comment_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class CommentTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)