質問編集履歴
3
paramsのメッセージを上部においた
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
を用意。bundle install済みです。
|
|
8
8
|
|
|
9
|
+
|
|
9
10
|
#【解決したいこと】画像の投稿だけDBに保存されません
|
|
10
11
|
|
|
11
12
|
できる◯
|
|
@@ -17,8 +18,25 @@
|
|
|
17
18
|
|
|
18
19
|
(バリデーションは現在かけていません)
|
|
19
20
|
|
|
20
|
-
#
|
|
21
|
+
#paramsのメッセージについて
|
|
22
|
+
```
|
|
23
|
+
42: private
|
|
24
|
+
43:
|
|
25
|
+
44: def post_params
|
|
26
|
+
45: params.require(:post).permit(:title, :content, images_attributes: [:src]).merge(user_id: current_user.id)
|
|
27
|
+
46: byebug
|
|
28
|
+
=> 47: end
|
|
29
|
+
48:
|
|
30
|
+
49: def set_post
|
|
31
|
+
50: @post = Post.find(params[:id])
|
|
32
|
+
51: end
|
|
33
|
+
(byebug) params
|
|
34
|
+
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"avP5DeA8ZSPhaUMkOvPmvW1rF7ML5bptV+cb6PMs3O5pcsBXzvHZtTVzWo0iWPC5crB/actaEN/AquKCzZY9ZA==", "post"=><ActionController::Parameters {"images_attributes"=>{"0"=>{"src"=>#<ActionDispatch::Http::UploadedFile:0x00007ff3b20f7438 @tempfile=#<Tempfile:/var/folders/ww/kfxy7w0565x8jj51gtp67gmr0000gn/T/RackMultipart20200312-43216-1o11cr3.png>, @original_filename="スクリーンショット 2020-03-04 16.04.20.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[images_attributes][0][src]\"; filename=\"\xE3\x82\xB9\xE3\x82\xAF\xE3\x83\xAA\xE3\x83\xBC\xE3\x83\xB3\xE3\x82\xB7\xE3\x83\xA7\xE3\x83\x83\xE3\x83\x88 2020-03-04 16.04.20.png\"\r\nContent-Type: image/png\r\n">}}, "title"=>"1234", "content"=>"5677"} permitted: false>, "commit"=>"Save", "controller"=>"posts", "action"=>"create"} permitted: false>
|
|
35
|
+
```
|
|
36
|
+
paramsまではデータが運ばれていると思いました。
|
|
21
37
|
|
|
38
|
+
#現状の疑わしい点の整理
|
|
39
|
+
|
|
22
40
|
**①paramsの値が返ってこないcreateアクションが怪しい・・・**
|
|
23
41
|
|
|
24
42
|
・createアクションがimageのデータを読み込まれてない?
|
|
@@ -59,25 +77,11 @@
|
|
|
59
77
|
byebug
|
|
60
78
|
end
|
|
61
79
|
```
|
|
62
|
-
↑paramsの値が返ってきました
|
|
80
|
+
↑この記述では、paramsの値が返ってきました(上記paramsのメッセージについてに記載済み)
|
|
63
81
|
|
|
64
|
-
```
|
|
65
|
-
42: private
|
|
66
|
-
43:
|
|
67
|
-
44: def post_params
|
|
68
|
-
45: params.require(:post).permit(:title, :content, images_attributes: [:src]).merge(user_id: current_user.id)
|
|
69
|
-
46: byebug
|
|
70
|
-
=> 47: end
|
|
71
|
-
48:
|
|
72
|
-
49: def set_post
|
|
73
|
-
50: @post = Post.find(params[:id])
|
|
74
|
-
51: end
|
|
75
|
-
(byebug) params
|
|
76
|
-
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"avP5DeA8ZSPhaUMkOvPmvW1rF7ML5bptV+cb6PMs3O5pcsBXzvHZtTVzWo0iWPC5crB/actaEN/AquKCzZY9ZA==", "post"=><ActionController::Parameters {"images_attributes"=>{"0"=>{"src"=>#<ActionDispatch::Http::UploadedFile:0x00007ff3b20f7438 @tempfile=#<Tempfile:/var/folders/ww/kfxy7w0565x8jj51gtp67gmr0000gn/T/RackMultipart20200312-43216-1o11cr3.png>, @original_filename="スクリーンショット 2020-03-04 16.04.20.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[images_attributes][0][src]\"; filename=\"\xE3\x82\xB9\xE3\x82\xAF\xE3\x83\xAA\xE3\x83\xBC\xE3\x83\xB3\xE3\x82\xB7\xE3\x83\xA7\xE3\x83\x83\xE3\x83\x88 2020-03-04 16.04.20.png\"\r\nContent-Type: image/png\r\n">}}, "title"=>"1234", "content"=>"5677"} permitted: false>, "commit"=>"Save", "controller"=>"posts", "action"=>"create"} permitted: false>
|
|
77
|
-
```
|
|
78
|
-
paramsまではデータが運ばれていると思いました。
|
|
79
82
|
|
|
80
83
|
|
|
84
|
+
|
|
81
85
|
#各ファイルのデータについて
|
|
82
86
|
|
|
83
87
|
|
2
参考にしたURLを追加しました
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -326,6 +326,7 @@
|
|
|
326
326
|
[【メモ】Strong Parametersの概要と配列データの扱い方について](https://qiita.com/_ayk_study/items/bf19ac188686584863e2)
|
|
327
327
|
[paramsを用いた投稿画面の作成と保存
|
|
328
328
|
](https://qiita.com/daisuke19840125/items/64c9fd2bc0cf6f6f7e4d)
|
|
329
|
+
[[学習用]Rails5:CarrierWaveで画像投稿+rmagickでサムネイル保存+画像名ランダム化](https://qiita.com/matcham/items/488ad5e94673c33bb545)
|
|
329
330
|
|
|
330
331
|
|
|
331
332
|
以上となります。
|
1
みずらい部分の修正
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -214,6 +214,48 @@
|
|
|
214
214
|
</div>
|
|
215
215
|
```
|
|
216
216
|
|
|
217
|
+
new.html.erb
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
<div>
|
|
221
|
+
<div class = "col-md-4 offset-md-4">
|
|
222
|
+
<h2 class = "text-center">new post</h2>
|
|
223
|
+
<%= render partial: "form" %>
|
|
224
|
+
</div>
|
|
225
|
+
<div class = "col-md-4 offset-4">
|
|
226
|
+
<%= link_to 'トップ', root_path %>
|
|
227
|
+
</div>
|
|
228
|
+
</div>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
index.html.erb
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
<%= render partial: "devise/shared/header" %>
|
|
235
|
+
|
|
236
|
+
<%# 会員登録、ログイン%>
|
|
237
|
+
<main role="main">
|
|
238
|
+
<section class="jumbotron text-center">
|
|
239
|
+
<div class="container">
|
|
240
|
+
<h3>ワンペタノート</h3>
|
|
241
|
+
<p class="lead text-muted">画像も投稿できる掲示板です</p>
|
|
242
|
+
<p>
|
|
243
|
+
<% if user_signed_in? %>
|
|
244
|
+
<%= link_to "新規投稿", new_post_path, class: 'btn btn-primary my-2' %>
|
|
245
|
+
<%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: 'btn btn-primary my-2' %>
|
|
246
|
+
<% else %>
|
|
247
|
+
<%= link_to "新規登録", new_user_registration_path, class: 'btn btn-primary my-2' %>
|
|
248
|
+
<%= link_to "ログイン", new_user_session_path, class: 'btn btn-primary my-2' %>
|
|
249
|
+
<% end %>
|
|
250
|
+
</p>
|
|
251
|
+
</div>
|
|
252
|
+
</section>
|
|
253
|
+
|
|
254
|
+
<%= render partial: 'post', collection: @posts %>
|
|
255
|
+
<%= render :partial => "devise/shared/footer" %>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
③コントローラー
|
|
217
259
|
post_controller.rb
|
|
218
260
|
|
|
219
261
|
```controller
|
|
@@ -271,46 +313,7 @@
|
|
|
271
313
|
end
|
|
272
314
|
```
|
|
273
315
|
|
|
274
|
-
new.html.erb
|
|
275
316
|
|
|
276
|
-
```
|
|
277
|
-
<div>
|
|
278
|
-
<div class = "col-md-4 offset-md-4">
|
|
279
|
-
<h2 class = "text-center">new post</h2>
|
|
280
|
-
<%= render partial: "form" %>
|
|
281
|
-
</div>
|
|
282
|
-
<div class = "col-md-4 offset-4">
|
|
283
|
-
<%= link_to 'トップ', root_path %>
|
|
284
|
-
</div>
|
|
285
|
-
</div>
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
index.html.erb
|
|
289
|
-
|
|
290
|
-
```
|
|
291
|
-
<%= render partial: "devise/shared/header" %>
|
|
292
|
-
|
|
293
|
-
<%# 会員登録、ログイン%>
|
|
294
|
-
<main role="main">
|
|
295
|
-
<section class="jumbotron text-center">
|
|
296
|
-
<div class="container">
|
|
297
|
-
<h3>ワンペタノート</h3>
|
|
298
|
-
<p class="lead text-muted">画像も投稿できる掲示板です</p>
|
|
299
|
-
<p>
|
|
300
|
-
<% if user_signed_in? %>
|
|
301
|
-
<%= link_to "新規投稿", new_post_path, class: 'btn btn-primary my-2' %>
|
|
302
|
-
<%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: 'btn btn-primary my-2' %>
|
|
303
|
-
<% else %>
|
|
304
|
-
<%= link_to "新規登録", new_user_registration_path, class: 'btn btn-primary my-2' %>
|
|
305
|
-
<%= link_to "ログイン", new_user_session_path, class: 'btn btn-primary my-2' %>
|
|
306
|
-
<% end %>
|
|
307
|
-
</p>
|
|
308
|
-
</div>
|
|
309
|
-
</section>
|
|
310
|
-
|
|
311
|
-
<%= render partial: 'post', collection: @posts %>
|
|
312
|
-
<%= render :partial => "devise/shared/footer" %>
|
|
313
|
-
```
|
|
314
317
|
#ER図は以下のように考えてます。
|
|
315
318
|
|
|
316
319
|
余計な情報かもしれませんが・・・おかしな点がございましたらご指摘いただけますと幸いです。
|