質問編集履歴
2
問題箇所viewはposts#showでした。controllerとしてcomments_controllerを載せておりましたが、posts_controllerを載せるべきでしたので、差し替えます
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,35 +34,59 @@
|
|
34
34
|
belongs_to :post
|
35
35
|
end
|
36
36
|
```
|
37
|
+
posts#show viewにコメントを表示させています。
|
37
|
-
|
38
|
+
posts_controller.rb
|
38
39
|
```
|
39
|
-
class
|
40
|
+
class PostsController < ApplicationController
|
40
41
|
|
41
|
-
before_action :set_post
|
42
|
+
before_action :set_post, only: [:show, :edit, :update, :destroy]
|
42
43
|
|
44
|
+
def index
|
45
|
+
@posts = Post.all.order("created_at DESC")
|
46
|
+
end
|
47
|
+
|
48
|
+
def new
|
49
|
+
@post = Post.new
|
50
|
+
end
|
51
|
+
|
43
52
|
def create
|
44
|
-
@
|
53
|
+
@post = Post.new(post_params)
|
45
|
-
if @
|
54
|
+
if @post.save
|
46
|
-
|
55
|
+
redirect_to post_path(@post)
|
47
56
|
else
|
48
|
-
render
|
57
|
+
render :new
|
49
58
|
end
|
50
59
|
end
|
51
60
|
|
61
|
+
def show
|
62
|
+
@comment = @post.comments.new
|
63
|
+
@comments = @post.comments
|
64
|
+
end
|
65
|
+
|
66
|
+
def edit
|
67
|
+
end
|
68
|
+
|
69
|
+
def update
|
70
|
+
if @post.update(post_params)
|
71
|
+
redirect_to post_path(@post)
|
72
|
+
else
|
73
|
+
render :edit
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
52
77
|
def destroy
|
53
|
-
@comment = @post.comments.find(params[:id])
|
54
|
-
@
|
78
|
+
@post.destroy
|
55
|
-
redirect_to
|
79
|
+
redirect_to root_path
|
56
80
|
end
|
57
81
|
|
58
82
|
private
|
59
83
|
|
60
84
|
def set_post
|
61
|
-
@post = Post.find(params[:
|
85
|
+
@post = Post.find(params[:id])
|
62
86
|
end
|
63
87
|
|
64
|
-
def
|
88
|
+
def post_params
|
65
|
-
params.require(:
|
89
|
+
params.require(:post).permit(:title, :body)
|
66
90
|
end
|
67
91
|
|
68
92
|
end
|
1
モデルおよびコントローラのコードを追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -22,6 +22,85 @@
|
|
22
22
|
```
|
23
23
|
保存されているcomment数より1回分多くレンダリングされるため、最後に”Delete”のみが一つ表示されてしまいます。
|
24
24
|
|
25
|
+
モデル、コントローラーも追記します。
|
26
|
+
新規コメントは非同期通信で行っていますが、こちらは問題なく動作します。
|
27
|
+
ページロードまたはリロード時に上記問題が発生します。
|
28
|
+
|
29
|
+
comment.rb
|
30
|
+
```
|
31
|
+
class Comment < ApplicationRecord
|
32
|
+
validates :name, presence: true
|
33
|
+
validates :body, presence: true
|
34
|
+
belongs_to :post
|
35
|
+
end
|
36
|
+
```
|
37
|
+
comments_controller.rb
|
38
|
+
```
|
39
|
+
class CommentsController < ApplicationController
|
40
|
+
|
41
|
+
before_action :set_post
|
42
|
+
|
43
|
+
def create
|
44
|
+
@comment = @post.comments.new(comment_params)
|
45
|
+
if @comment.save
|
46
|
+
render json: {result: "ok", comment: @comment, post_id: @post.id}
|
47
|
+
else
|
48
|
+
render json: {result: "ng", msg: @comment.errors.messages}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def destroy
|
53
|
+
@comment = @post.comments.find(params[:id])
|
54
|
+
@comment.destroy
|
55
|
+
redirect_to post_path(@post)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def set_post
|
61
|
+
@post = Post.find(params[:post_id])
|
62
|
+
end
|
63
|
+
|
64
|
+
def comment_params
|
65
|
+
params.require(:comment).permit(:name, :body)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
```
|
70
|
+
不要かもしれませんが、comment.js
|
71
|
+
```
|
72
|
+
$(document).on("ajax:success", "#new-comment", function(e) {
|
73
|
+
// create new comment html
|
74
|
+
var html = buildHTML(e.detail[0]);
|
75
|
+
$('.comments').append(html);
|
76
|
+
$('.commentform_name').val('');
|
77
|
+
$('.commentform_body').val('');
|
78
|
+
// scroll to the page bottom
|
79
|
+
$('html, body').animate({
|
80
|
+
scrollTop: $(document).height()
|
81
|
+
},1500);
|
82
|
+
alert("send your comment")
|
83
|
+
console.log(e.detail[0]);
|
84
|
+
});
|
85
|
+
$(document).on("ajax:error", "#new-comment", function(e) {
|
86
|
+
console.log(e.detail[2]);
|
87
|
+
});
|
88
|
+
|
89
|
+
|
90
|
+
function buildHTML(data){
|
91
|
+
var html = '<h3 class ="font-weight-bold mt-4">'
|
92
|
+
+ data.comment.name
|
93
|
+
+ '<a class ="text-secondary small ml-2" data-method="delete" href="/posts/' + data.post_id + '/comments/' + data.comment.id + '">Delete</a>'
|
94
|
+
+ '</h3>\n'
|
95
|
+
+ '<div class ="mt-2">'
|
96
|
+
+ data.comment.body
|
97
|
+
+ '</div>\n';
|
98
|
+
return html;
|
99
|
+
}
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
|
25
104
|
### 試したこと
|
26
105
|
```comment_counter``` で確認すると、0〜データ数までレンダリングされていることが分かりました。原因は何でしょうか?
|
27
106
|
### 補足情報(FW/ツールのバージョンなど)
|