質問編集履歴

10

修正

2020/04/11 11:11

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,9 @@
6
6
 
7
7
 
8
8
 
9
- 以下の写真のような新規投稿画面にて写真を選択後、投稿ボタンを押しても、
9
+ 新規投稿画面にて写真を選択後、投稿ボタンを押しても、
10
-
10
+
11
- 写真を入力してくださいとエラーメッセージが出てしまいます。
11
+ Unpermitted parameter: :images」が出てしまい投稿に失敗します。
12
12
 
13
13
 
14
14
 
@@ -20,14 +20,6 @@
20
20
 
21
21
 
22
22
 
23
-
24
-
25
- ![イメージ説明](f12c93ccb1b6a6c38a4e994d51c328ab.png)
26
-
27
-
28
-
29
-
30
-
31
23
  ### 該当のソースコード
32
24
 
33
25
 
@@ -42,28 +34,6 @@
42
34
 
43
35
 
44
36
 
45
- def index
46
-
47
- @q = Post.ransack(params[:q])
48
-
49
- @posts = if @tag_name = params[:tag_name]
50
-
51
- Post.tag_search(params[:tag_name]).page(params[:page]).per(PER)
52
-
53
- elsif params[:q]
54
-
55
- @q.result.order('created_at DESC').page(params[:page]).per(PER)
56
-
57
- else
58
-
59
- Post.page(params[:page]).per(PER)
60
-
61
- end
62
-
63
- end
64
-
65
-
66
-
67
37
  def new
68
38
 
69
39
  @post = current_user.posts.build
@@ -72,108 +42,50 @@
72
42
 
73
43
 
74
44
 
75
- def show
45
+ def create
46
+
47
+ @post = current_user.posts.build(posts_params)
48
+
49
+ if @post.save
50
+
51
+ redirect_to current_user, flash: { success: "投稿しました。" }
52
+
53
+ else
54
+
55
+ render :new
56
+
57
+ end
58
+
59
+ end
60
+
61
+
62
+
63
+ private
64
+
65
+
66
+
67
+ def posts_params
68
+
69
+ params.require(:post).permit(
70
+
71
+ :title,
72
+
73
+ :content,
74
+
75
+ { images: [] },
76
+
77
+ :tag_list
78
+
79
+ )
80
+
81
+ end
82
+
83
+
84
+
85
+ def correct_user
76
86
 
77
87
  @post = Post.find(params[:id])
78
88
 
79
- current_user&.footprints&.create(post_id: @post.id)
80
-
81
- @like = Like.new
82
-
83
- @comment = Comment.new
84
-
85
- @comments = @post.comments
86
-
87
- end
88
-
89
-
90
-
91
- def create
92
-
93
- @post = current_user.posts.build(posts_params)
94
-
95
- if @post.save
96
-
97
- redirect_to current_user, flash: { success: "投稿しました。" }
98
-
99
- else
100
-
101
- render :new
102
-
103
- end
104
-
105
- end
106
-
107
-
108
-
109
- def edit
110
-
111
- p params[:id]
112
-
113
- @post = Post.find(params[:id])
114
-
115
- end
116
-
117
-
118
-
119
- def update
120
-
121
- @post = Post.find(params[:id])
122
-
123
- if @post.update(posts_params)
124
-
125
- redirect_to root_path
126
-
127
- else
128
-
129
- render :edit
130
-
131
- end
132
-
133
- end
134
-
135
-
136
-
137
- def destroy
138
-
139
- @post = Post.find(params[:id])
140
-
141
- @post.destroy
142
-
143
- flash.now[:success] = "投稿を削除しました。"
144
-
145
- end
146
-
147
-
148
-
149
-
150
-
151
- private
152
-
153
-
154
-
155
- def posts_params
156
-
157
- params.require(:post).permit(
158
-
159
- :title,
160
-
161
- :content,
162
-
163
- { images: [] },
164
-
165
- :tag_list
166
-
167
- )
168
-
169
- end
170
-
171
-
172
-
173
- def correct_user
174
-
175
- @post = Post.find(params[:id])
176
-
177
89
  redirect_to(root_url) unless @post.user == current_user
178
90
 
179
91
  end
@@ -288,62 +200,10 @@
288
200
 
289
201
  validates :title, presence: true, length: { maximum: 50 }
290
202
 
291
- # like関係
292
-
293
- has_many :likes, dependent: :destroy
294
-
295
- has_many :liked_users, through: :likes, source: :user
296
-
297
- # comment
298
-
299
- has_many :comments, dependent: :destroy
300
-
301
- # tag
302
-
303
- acts_as_taggable
304
-
305
-
306
-
307
- def self.search(search)
308
-
309
- where(['title LIKE ?', "%#{search}%"])
310
-
311
- end
312
-
313
-
314
-
315
- def self.tag_search(tag_name)
316
-
317
- tagged_with(tag_name.to_s)
318
-
319
- end
320
-
321
203
  end
322
204
 
323
205
  ```
324
206
 
325
- ```DB
326
-
327
- create_table "posts", force: :cascade do |t|
328
-
329
- t.text "content"
330
-
331
- t.integer "user_id"
332
-
333
- t.datetime "created_at", precision: 6, null: false
334
-
335
- t.datetime "updated_at", precision: 6, null: false
336
-
337
- t.string "images"
338
-
339
- t.string "title"
340
-
341
- t.index ["user_id"], name: "index_posts_on_user_id"
342
-
343
- end
344
-
345
- ```
346
-
347
207
 
348
208
 
349
209
  ```Log

9

修正

2020/04/11 11:11

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -282,7 +282,7 @@
282
282
 
283
283
  mount_uploaders :images, ImageUploader
284
284
 
285
- validates :images, presence: { message: 'が選択れてません' }
285
+ validates :images, presence: { message: 'を入力してください' }
286
286
 
287
287
  validates :user_id, presence: true
288
288
 

8

追記

2020/04/08 15:40

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -272,6 +272,56 @@
272
272
 
273
273
  ```
274
274
 
275
+ ```model
276
+
277
+ class Post < ApplicationRecord
278
+
279
+ belongs_to :user
280
+
281
+ default_scope -> { order(created_at: :desc) }
282
+
283
+ mount_uploaders :images, ImageUploader
284
+
285
+ validates :images, presence: { message: 'が選択されていません' }
286
+
287
+ validates :user_id, presence: true
288
+
289
+ validates :title, presence: true, length: { maximum: 50 }
290
+
291
+ # like関係
292
+
293
+ has_many :likes, dependent: :destroy
294
+
295
+ has_many :liked_users, through: :likes, source: :user
296
+
297
+ # comment
298
+
299
+ has_many :comments, dependent: :destroy
300
+
301
+ # tag
302
+
303
+ acts_as_taggable
304
+
305
+
306
+
307
+ def self.search(search)
308
+
309
+ where(['title LIKE ?', "%#{search}%"])
310
+
311
+ end
312
+
313
+
314
+
315
+ def self.tag_search(tag_name)
316
+
317
+ tagged_with(tag_name.to_s)
318
+
319
+ end
320
+
321
+ end
322
+
323
+ ```
324
+
275
325
  ```DB
276
326
 
277
327
  create_table "posts", force: :cascade do |t|
@@ -322,8 +372,6 @@
322
372
 
323
373
 
324
374
 
325
-
326
-
327
375
  ```
328
376
 
329
377
 

7

修正

2020/04/08 15:38

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -348,12 +348,30 @@
348
348
 
349
349
  ```Log
350
350
 
351
+ Started POST "/posts" for ::1 at 2020-04-09 00:34:59 +0900
352
+
353
+ (0.1ms) SELECT sqlite_version(*)
354
+
351
355
  Processing by PostsController#create as HTML
352
356
 
353
- Parameters: {"authenticity_token"=>"RvtJFOLqX+KnzLHdCme5e5HTjIw4kJvzEeyMUijBAnBVU/fNcKPPOXX+NczpcP4QEJv4TsIVgEgLKWaHdchjZw==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd71ac7cbf0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-9q4fxi.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
354
-
355
- User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
357
+ Parameters: {"authenticity_token"=>"9Kk54+zTJ83/yISpcCZBXB46Qy5dBMoHMQwiNtGLAN9VaC2E444uhX9xNRJ2efLN9cGpBwSIoJdhjYyMCVDgYQ==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd720081408 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-885wsk.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
358
+
359
+ User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
356
360
 
357
361
  Unpermitted parameter: :images
358
362
 
363
+ Rendering posts/new.html.erb within layouts/application
364
+
365
+ Rendered layouts/_error_message.html.erb (Duration: 0.3ms | Allocations: 121)
366
+
367
+ Rendered posts/new.html.erb within layouts/application (Duration: 7.2ms | Allocations: 2400)
368
+
369
+ [Webpacker] Everything's up-to-date. Nothing to do
370
+
371
+ Rendered layouts/_header.html.erb (Duration: 0.8ms | Allocations: 500)
372
+
373
+ Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
374
+
375
+ Completed 200 OK in 66ms (Views: 29.1ms | ActiveRecord: 0.5ms | Allocations: 36059)
376
+
359
- ```
377
+ ```

6

修正

2020/04/08 15:36

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -344,8 +344,16 @@
344
344
 
345
345
 
346
346
 
347
- コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下写真のようなエラー画面に遷移てします
348
-
349
-
350
-
351
- ![![イメージ説明](d7ef91db3e0bc06c93dc41f43f970981.png)](c42f47ff7d09456fec27dbcbad257683.png)
347
+ コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、同様のエラーログが表示されま
348
+
349
+ ```Log
350
+
351
+ Processing by PostsController#create as HTML
352
+
353
+ Parameters: {"authenticity_token"=>"RvtJFOLqX+KnzLHdCme5e5HTjIw4kJvzEeyMUijBAnBVU/fNcKPPOXX+NczpcP4QEJv4TsIVgEgLKWaHdchjZw==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd71ac7cbf0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-9q4fxi.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
354
+
355
+ User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
356
+
357
+ Unpermitted parameter: :images
358
+
359
+ ```

5

追記 編集

2020/04/08 15:33

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -344,7 +344,7 @@
344
344
 
345
345
 
346
346
 
347
- コントローラのposts_params内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
347
+ コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
348
348
 
349
349
 
350
350
 

4

追記

2020/04/08 11:43

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -341,3 +341,11 @@
341
341
 
342
342
 
343
343
  ### 追記
344
+
345
+
346
+
347
+ コントローラの「posts_params」内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
348
+
349
+
350
+
351
+ ![![イメージ説明](d7ef91db3e0bc06c93dc41f43f970981.png)](c42f47ff7d09456fec27dbcbad257683.png)

3

追記

2020/04/08 11:40

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -337,3 +337,7 @@
337
337
  Rails:6.0.2.1
338
338
 
339
339
  写真投稿機能:carrierwave
340
+
341
+
342
+
343
+ ### 追記

2

誤字

2020/04/08 11:33

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
File without changes
test CHANGED
@@ -296,6 +296,36 @@
296
296
 
297
297
 
298
298
 
299
+ ```Log
300
+
301
+ Processing by PostsController#create as HTML
302
+
303
+ Parameters: {"authenticity_token"=>"JNyHLThmTh3Fh1seMb9p2f5x2s5MmPaIieMCqfAG706P061Rxgukgnk6p3AyKg2LFmYFa9FTFdRy6oEthU+CoQ==", "post"=>{"images"=>#<ActionDispatch::Http::UploadedFile:0x00007f942c706df0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200408-84512-bxsvru.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">, "title"=>"あ", "content"=>"あ\r\n", "tag_list"=>"あ"}, "commit"=>"投稿"}
304
+
305
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
306
+
307
+ Unpermitted parameter: :images
308
+
309
+ Rendering posts/new.html.erb within layouts/application
310
+
311
+ Rendered layouts/_error_message.html.erb (Duration: 0.2ms | Allocations: 121)
312
+
313
+ Rendered posts/new.html.erb within layouts/application (Duration: 2.9ms | Allocations: 2392)
314
+
315
+ [Webpacker] Everything's up-to-date. Nothing to do
316
+
317
+ Rendered layouts/_header.html.erb (Duration: 0.5ms | Allocations: 434)
318
+
319
+ Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
320
+
321
+ Completed 200 OK in 14ms (Views: 8.9ms | ActiveRecord: 0.2ms | Allocations: 10914)
322
+
323
+
324
+
325
+
326
+
327
+ ```
328
+
299
329
 
300
330
 
301
331
  ### 補足情報(FW/ツールのバージョンなど)

1

誤字

2020/04/07 16:10

投稿

Kazu_p8619
Kazu_p8619

スコア9

test CHANGED
@@ -1 +1 @@
1
- [Ruby on Rails 6]写真投稿に失敗してしまう。
1
+ [Ruby on Rails 6]写真投稿に失敗してしまう。
test CHANGED
@@ -6,13 +6,17 @@
6
6
 
7
7
 
8
8
 
9
- 以下の写真のような新規投稿画面にて写真を選択投稿ボタンを押しても、
9
+ 以下の写真のような新規投稿画面にて写真を選択後、投稿ボタンを押しても、
10
10
 
11
11
  「写真を入力してください」とエラーメッセージが出てしまいます。
12
12
 
13
13
 
14
14
 
15
+ プレビューにも表示されているように写真の選択はできているはずなのですが、
16
+
15
- 様々な方法で試行してみたので原因がわからないため、ご質問させていただきます。
17
+ 投稿に失敗原因がわからないため、ご質問させていただきます。
18
+
19
+ 初心者のため至らない点が多いですがよろしくお願いします。
16
20
 
17
21
 
18
22