質問編集履歴
2
問題箇所viewはposts#showでした。controllerとしてcomments_controllerを載せておりましたが、posts_controllerを載せるべきでしたので、差し替えます
test
CHANGED
File without changes
|
test
CHANGED
@@ -70,29 +70,47 @@
|
|
70
70
|
|
71
71
|
```
|
72
72
|
|
73
|
+
posts#show viewにコメントを表示させています。
|
74
|
+
|
73
|
-
|
75
|
+
posts_controller.rb
|
74
|
-
|
76
|
+
|
75
|
-
```
|
77
|
+
```
|
76
|
-
|
78
|
+
|
77
|
-
class
|
79
|
+
class PostsController < ApplicationController
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
|
81
|
+
|
82
|
+
|
81
|
-
before_action :set_post
|
83
|
+
before_action :set_post, only: [:show, :edit, :update, :destroy]
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def index
|
88
|
+
|
89
|
+
@posts = Post.all.order("created_at DESC")
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
def new
|
96
|
+
|
97
|
+
@post = Post.new
|
98
|
+
|
99
|
+
end
|
82
100
|
|
83
101
|
|
84
102
|
|
85
103
|
def create
|
86
104
|
|
87
|
-
@
|
105
|
+
@post = Post.new(post_params)
|
88
|
-
|
106
|
+
|
89
|
-
if @
|
107
|
+
if @post.save
|
90
|
-
|
108
|
+
|
91
|
-
re
|
109
|
+
redirect_to post_path(@post)
|
92
110
|
|
93
111
|
else
|
94
112
|
|
95
|
-
render
|
113
|
+
render :new
|
96
114
|
|
97
115
|
end
|
98
116
|
|
@@ -100,13 +118,43 @@
|
|
100
118
|
|
101
119
|
|
102
120
|
|
121
|
+
def show
|
122
|
+
|
123
|
+
@comment = @post.comments.new
|
124
|
+
|
125
|
+
@comments = @post.comments
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def edit
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
def update
|
138
|
+
|
139
|
+
if @post.update(post_params)
|
140
|
+
|
141
|
+
redirect_to post_path(@post)
|
142
|
+
|
143
|
+
else
|
144
|
+
|
145
|
+
render :edit
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
|
103
153
|
def destroy
|
104
154
|
|
105
|
-
@comment = @post.comments.find(params[:id])
|
106
|
-
|
107
|
-
@
|
155
|
+
@post.destroy
|
108
|
-
|
156
|
+
|
109
|
-
redirect_to
|
157
|
+
redirect_to root_path
|
110
158
|
|
111
159
|
end
|
112
160
|
|
@@ -118,15 +166,15 @@
|
|
118
166
|
|
119
167
|
def set_post
|
120
168
|
|
121
|
-
@post = Post.find(params[:
|
169
|
+
@post = Post.find(params[:id])
|
122
|
-
|
170
|
+
|
123
|
-
end
|
171
|
+
end
|
124
|
-
|
125
|
-
|
126
|
-
|
172
|
+
|
173
|
+
|
174
|
+
|
127
|
-
def
|
175
|
+
def post_params
|
128
|
-
|
176
|
+
|
129
|
-
params.require(:
|
177
|
+
params.require(:post).permit(:title, :body)
|
130
178
|
|
131
179
|
end
|
132
180
|
|
1
モデルおよびコントローラのコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -46,6 +46,164 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
+
モデル、コントローラーも追記します。
|
50
|
+
|
51
|
+
新規コメントは非同期通信で行っていますが、こちらは問題なく動作します。
|
52
|
+
|
53
|
+
ページロードまたはリロード時に上記問題が発生します。
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
comment.rb
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
class Comment < ApplicationRecord
|
62
|
+
|
63
|
+
validates :name, presence: true
|
64
|
+
|
65
|
+
validates :body, presence: true
|
66
|
+
|
67
|
+
belongs_to :post
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
comments_controller.rb
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
class CommentsController < ApplicationController
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
before_action :set_post
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def create
|
86
|
+
|
87
|
+
@comment = @post.comments.new(comment_params)
|
88
|
+
|
89
|
+
if @comment.save
|
90
|
+
|
91
|
+
render json: {result: "ok", comment: @comment, post_id: @post.id}
|
92
|
+
|
93
|
+
else
|
94
|
+
|
95
|
+
render json: {result: "ng", msg: @comment.errors.messages}
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def destroy
|
104
|
+
|
105
|
+
@comment = @post.comments.find(params[:id])
|
106
|
+
|
107
|
+
@comment.destroy
|
108
|
+
|
109
|
+
redirect_to post_path(@post)
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def set_post
|
120
|
+
|
121
|
+
@post = Post.find(params[:post_id])
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
def comment_params
|
128
|
+
|
129
|
+
params.require(:comment).permit(:name, :body)
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
不要かもしれませんが、comment.js
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
$(document).on("ajax:success", "#new-comment", function(e) {
|
144
|
+
|
145
|
+
// create new comment html
|
146
|
+
|
147
|
+
var html = buildHTML(e.detail[0]);
|
148
|
+
|
149
|
+
$('.comments').append(html);
|
150
|
+
|
151
|
+
$('.commentform_name').val('');
|
152
|
+
|
153
|
+
$('.commentform_body').val('');
|
154
|
+
|
155
|
+
// scroll to the page bottom
|
156
|
+
|
157
|
+
$('html, body').animate({
|
158
|
+
|
159
|
+
scrollTop: $(document).height()
|
160
|
+
|
161
|
+
},1500);
|
162
|
+
|
163
|
+
alert("send your comment")
|
164
|
+
|
165
|
+
console.log(e.detail[0]);
|
166
|
+
|
167
|
+
});
|
168
|
+
|
169
|
+
$(document).on("ajax:error", "#new-comment", function(e) {
|
170
|
+
|
171
|
+
console.log(e.detail[2]);
|
172
|
+
|
173
|
+
});
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
function buildHTML(data){
|
180
|
+
|
181
|
+
var html = '<h3 class ="font-weight-bold mt-4">'
|
182
|
+
|
183
|
+
+ data.comment.name
|
184
|
+
|
185
|
+
+ '<a class ="text-secondary small ml-2" data-method="delete" href="/posts/' + data.post_id + '/comments/' + data.comment.id + '">Delete</a>'
|
186
|
+
|
187
|
+
+ '</h3>\n'
|
188
|
+
|
189
|
+
+ '<div class ="mt-2">'
|
190
|
+
|
191
|
+
+ data.comment.body
|
192
|
+
|
193
|
+
+ '</div>\n';
|
194
|
+
|
195
|
+
return html;
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
49
207
|
### 試したこと
|
50
208
|
|
51
209
|
```comment_counter``` で確認すると、0〜データ数までレンダリングされていることが分かりました。原因は何でしょうか?
|