File tree 14 files changed +111
-5
lines changed
14 files changed +111
-5
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,14 @@ def update
31
31
render :edit , status : :unprocessable_entity
32
32
end
33
33
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
+
35
42
private
36
43
def article_params
37
44
params . require ( :article ) . permit ( :title , :body )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ module CommentsHelper
2
+ end
Original file line number Diff line number Diff line change 1
1
class Article < ApplicationRecord
2
+ has_many :comments
2
3
validates :title , presence : true
3
4
validates :body , presence : true , length : { minimum : 10 }
4
5
end
Original file line number Diff line number Diff line change
1
+ class Comment < ApplicationRecord
2
+ belongs_to :article
3
+ end
Original file line number Diff line number Diff line change 1
1
< h1 > <%= @article . title %> </ h1 >
2
2
3
3
< p > <%= @article . body %> </ p >
4
+
4
5
< 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' %>
Original file line number Diff line number Diff line change
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 %>
Original file line number Diff line number Diff line change
1
+ < p >
2
+ < strong > Commenter:</ strong >
3
+ <%= comment . commenter %>
4
+ </ p >
5
+
6
+ < p >
7
+ < strong > Comment:</ strong >
8
+ <%= comment . body %>
9
+ </ p >
Original file line number Diff line number Diff line change 1
1
Rails . application . routes . draw do
2
2
root "articles#index"
3
3
4
- resources :articles
4
+ resources :articles do
5
+ resources :comments
5
6
end
7
+ end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ require "test_helper"
2
+
3
+ class CommentsControllerTest < ActionDispatch ::IntegrationTest
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ require "test_helper"
2
+
3
+ class CommentTest < ActiveSupport ::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
You can’t perform that action at this time.
0 commit comments