質問編集履歴
1
質問のアングルを変えた。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
u
|
1
|
+
Ruby - コメントがDBに保存されない
|
test
CHANGED
@@ -2,33 +2,245 @@
|
|
2
2
|
|
3
3
|
rails でブログを投稿できるwebアプリケーションを作っていてコメント機能を実装しています。
|
4
4
|
|
5
|
-
コメントを投稿し
|
5
|
+
コメントを投稿したいのですが、保存されず投稿ボタンを押してもそのままになっています。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
|
14
|
-
|
15
|
-
|
8
|
+
|
16
|
-
|
17
|
-
|
9
|
+
|
18
|
-
|
19
|
-
```
|
20
10
|
|
21
11
|
|
22
12
|
|
23
13
|
# 試してみたこと
|
24
14
|
|
25
|
-
|
15
|
+
まず commentのコントローラにて binding.pry を使って paramsに値が入っているか確認しました。
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
2: def create
|
22
|
+
|
23
|
+
=> 3: binding.pry
|
24
|
+
|
25
|
+
4: @post = Post.find(params[:post_id])
|
26
|
+
|
27
|
+
5: @comment = Comment.new(comment_params)
|
28
|
+
|
29
|
+
6: if @comment.save
|
30
|
+
|
31
|
+
7: redirect_to post_path(@comment.post)
|
32
|
+
|
33
|
+
8: else
|
34
|
+
|
35
|
+
9: @comments = @post.comments
|
36
|
+
|
37
|
+
10: render "posts/show"
|
38
|
+
|
39
|
+
11: end
|
40
|
+
|
41
|
+
12: end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
[1] pry(#<CommentsController>)> params
|
46
|
+
|
47
|
+
=> <ActionController::Parameters {"authenticity_token"=>"CGJ6VwkXu2JAnQgGwOFKrnfWCdjIJlj4gsqqq6tVHP91rU4t5HA0zq5WZspb7d0czDYuO1qhPR6/K+MBrpEtcw==", "comment"=>{"text"=>"good"}, "commit"=>"Send", "controller"=>"comments", "action"=>"create", "post_id"=>"4"} permitted: false>
|
48
|
+
|
49
|
+
[2] pry(#<CommentsController>)> params[:text]
|
50
|
+
|
51
|
+
=> nil
|
52
|
+
|
53
|
+
[3] pry(#<CommentsController>)> params[:comment][:text]
|
54
|
+
|
55
|
+
=> "good"
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
みるとgoodと入力した値はしっかり入っています。
|
60
|
+
|
61
|
+
ですがなぜpermitted:falseになっているのでしょう。
|
26
62
|
|
27
63
|
|
28
64
|
|
29
65
|
# 該当のソースコード
|
30
66
|
|
67
|
+
|
68
|
+
|
69
|
+
コメントのコントローラです。
|
70
|
+
|
71
|
+
`app/controllers/comments_controller.rb`
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
class CommentsController < ApplicationController
|
76
|
+
|
77
|
+
def create
|
78
|
+
|
79
|
+
@post = Post.find(params[:post_id])
|
80
|
+
|
81
|
+
@comment = Comment.new(comment_params)
|
82
|
+
|
83
|
+
if @comment.save
|
84
|
+
|
85
|
+
redirect_to post_path(@comment.post)
|
86
|
+
|
87
|
+
else
|
88
|
+
|
89
|
+
@comments = @post.comments
|
90
|
+
|
91
|
+
render "posts/show"
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def comment_params
|
102
|
+
|
103
|
+
params.require(:comment).permit(:text).merge(user_id: current_user.id, post_id: params[:post_id])
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
postのコントローラです。
|
112
|
+
|
113
|
+
`app/controllers/posts_controller.rb`
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
class PostsController < ApplicationController
|
118
|
+
|
119
|
+
before_action :authenticate_user!, except: :index
|
120
|
+
|
121
|
+
before_action :set_post, only: [:show, :edit, :update, :destroy]
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def index
|
126
|
+
|
127
|
+
@posts = Post.all
|
128
|
+
|
129
|
+
@tags = Post.tag_counts_on(:tags).most_used(20)
|
130
|
+
|
131
|
+
if @tag = params[:tag]
|
132
|
+
|
133
|
+
@post = Post.tagged_with(params[:tag])
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def new
|
142
|
+
|
143
|
+
@post = Post.new
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def create
|
150
|
+
|
151
|
+
@post = Post.new(post_params)
|
152
|
+
|
153
|
+
if @post.valid?
|
154
|
+
|
155
|
+
@post.save
|
156
|
+
|
157
|
+
redirect_to root_path
|
158
|
+
|
159
|
+
else
|
160
|
+
|
161
|
+
render :new
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
def show
|
170
|
+
|
31
|
-
|
171
|
+
@tags = @post.tag_counts_on(:tags)
|
172
|
+
|
173
|
+
@comment = Comment.new
|
174
|
+
|
175
|
+
@comments = @post.comments.includes(:user)
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def edit
|
182
|
+
|
183
|
+
unless @post.user.id == current_user.id
|
184
|
+
|
185
|
+
redirect_to root_path
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def update
|
194
|
+
|
195
|
+
if @post.update(post_params)
|
196
|
+
|
197
|
+
redirect_to root_path
|
198
|
+
|
199
|
+
else
|
200
|
+
|
201
|
+
render :edit
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
def destroy
|
210
|
+
|
211
|
+
if @post.destroy
|
212
|
+
|
213
|
+
redirect_to root_path
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
private
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
def set_post
|
226
|
+
|
227
|
+
@post = Post.find(params[:id])
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
def post_params
|
234
|
+
|
235
|
+
params.require(:post).permit(:concept, :title, :image, :tag_list).merge(user_id: current_user.id)
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
一応ビューファイルも載せておきます。
|
32
244
|
|
33
245
|
`app/views/posts/show.html.erb`
|
34
246
|
|
@@ -78,7 +290,7 @@
|
|
78
290
|
|
79
291
|
<%= form_with model: [@post, @comment], local: true do |f|%>
|
80
292
|
|
81
|
-
<div class="
|
293
|
+
<div class="comment-box">
|
82
294
|
|
83
295
|
<%= f.label :text, "Comment" %><br />
|
84
296
|
|
@@ -120,182 +332,6 @@
|
|
120
332
|
|
121
333
|
```
|
122
334
|
|
123
|
-
コメントのコントローラです。
|
124
|
-
|
125
|
-
`app/controllers/comments_controller.rb`
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
|
-
class CommentsController < ApplicationController
|
130
|
-
|
131
|
-
def create
|
132
|
-
|
133
|
-
@post = Post.find(params[:post_id])
|
134
|
-
|
135
|
-
@comment = Comment.new(comment_params)
|
136
|
-
|
137
|
-
@comments = @post.comments
|
138
|
-
|
139
|
-
if @comment.save
|
140
|
-
|
141
|
-
redirect_to post_path(@comment.post)
|
142
|
-
|
143
|
-
else
|
144
|
-
|
145
|
-
@post = @comment.post_id
|
146
|
-
|
147
|
-
render "posts/show"
|
148
|
-
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
private
|
156
|
-
|
157
|
-
def comment_params
|
158
|
-
|
159
|
-
params.require(:comment).permit(:text).merge(user_id: current_user.id, post_id: params[:post_id])
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
end
|
164
|
-
|
165
|
-
```
|
166
|
-
|
167
|
-
postのコントローラです。コメントにおいてはshowアクションが該当すると思います。
|
168
|
-
|
169
|
-
`app/controllers/posts_controller.rb`
|
170
|
-
|
171
|
-
```
|
172
|
-
|
173
|
-
class PostsController < ApplicationController
|
174
|
-
|
175
|
-
before_action :authenticate_user!, except: :index
|
176
|
-
|
177
|
-
before_action :set_post, only: [:show, :edit, :update, :destroy]
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
def index
|
182
|
-
|
183
|
-
@posts = Post.all
|
184
|
-
|
185
|
-
@tags = Post.tag_counts_on(:tags).most_used(20)
|
186
|
-
|
187
|
-
if @tag = params[:tag]
|
188
|
-
|
189
|
-
@post = Post.tagged_with(params[:tag])
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
def new
|
198
|
-
|
199
|
-
@post = Post.new
|
200
|
-
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
def create
|
206
|
-
|
207
|
-
@post = Post.new(post_params)
|
208
|
-
|
209
|
-
if @post.valid?
|
210
|
-
|
211
|
-
@post.save
|
212
|
-
|
213
|
-
redirect_to root_path
|
214
|
-
|
215
|
-
else
|
216
|
-
|
217
|
-
render :new
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
end
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
def show
|
226
|
-
|
227
|
-
@tags = @post.tag_counts_on(:tags)
|
228
|
-
|
229
|
-
@comment = Comment.new
|
230
|
-
|
231
|
-
@comments = @post.comments.includes(:user)
|
232
|
-
|
233
|
-
end
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
def edit
|
238
|
-
|
239
|
-
unless @post.user.id == current_user.id
|
240
|
-
|
241
|
-
redirect_to root_path
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
def update
|
250
|
-
|
251
|
-
if @post.update(post_params)
|
252
|
-
|
253
|
-
redirect_to root_path
|
254
|
-
|
255
|
-
else
|
256
|
-
|
257
|
-
render :edit
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
def destroy
|
266
|
-
|
267
|
-
if @post.destroy
|
268
|
-
|
269
|
-
redirect_to root_path
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
end
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
private
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
def set_post
|
282
|
-
|
283
|
-
@post = Post.find(params[:id])
|
284
|
-
|
285
|
-
end
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
def post_params
|
290
|
-
|
291
|
-
params.require(:post).permit(:concept, :title, :image, :tag_list).merge(user_id: current_user.id)
|
292
|
-
|
293
|
-
end
|
294
|
-
|
295
|
-
end
|
296
|
-
|
297
|
-
```
|
298
|
-
|
299
335
|
|
300
336
|
|
301
337
|
|