質問編集履歴

7

指摘箇所の修正

2020/08/31 14:08

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -122,7 +122,7 @@
122
122
 
123
123
  @posts = Post.all
124
124
 
125
- @category_parent_arrays = Category.where(ancestry: nil)
125
+ @category_parent_array = Category.where(ancestry: nil)
126
126
 
127
127
  end
128
128
 
@@ -224,7 +224,7 @@
224
224
 
225
225
  def post_params
226
226
 
227
- params.require(:post).permit(:title, :body, :image)
227
+ params.require(:post).permit(:title, :body, :image, { post_categories: {} })
228
228
 
229
229
  end
230
230
 
@@ -294,6 +294,8 @@
294
294
 
295
295
  has_many :categories, through: :post_categories
296
296
 
297
+ accepts_nested_attributes_for :categories, allow_destroy: true
298
+
297
299
  end
298
300
 
299
301
  ```
@@ -304,12 +306,12 @@
304
306
 
305
307
  class Category < ApplicationRecord
306
308
 
307
- has_many :post_category, dependent: :destroy
308
-
309
- has_many :posts, through: :post_category
310
-
311
309
  has_ancestry
312
310
 
311
+ has_many :post_categories, dependent: :destroy
312
+
313
+ has_many :posts, through: :post_categories
314
+
313
315
  end
314
316
 
315
317
  ```

6

内容の修正

2020/08/31 14:08

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -4,27 +4,23 @@
4
4
 
5
5
 
6
6
 
7
- ##なぜカテゴリーもDBに登録したいか
8
-
9
- トップページから投稿を検索する機能をこれから実装するにあたり、カテゴリーから検索できるようにするためです。
10
-
11
- そのためには、投稿にカテゴリーのデータも必要だからです。
12
-
13
-
14
-
15
7
  ##状況
16
8
 
17
9
  railsで個人アプリを開発中で、スノボー版インスタグラムを作成しています。
18
10
 
19
11
 
20
12
 
21
- 現在の状況としては、投稿機能を実装し、カテゴリー以外のデータDBに値が取得されるようになっています。
13
+ 現在の状況としては、投稿機能カテゴリー以外は実装されています。
22
14
 
23
15
  (image,text,titleなど)
24
16
 
25
17
 
26
18
 
19
+ また、中間テーブルであるpost_categoryテーブルの紐付けがうまく行っていないのか、新規投稿される時にpost_idとcategory_idはデータベースに登録されずです。
20
+
21
+
22
+
27
- 以下hがビューの画像です。
23
+ 以下がビューの画像です。
28
24
 
29
25
  ![イメージ説明](04c4f2fefd61e3d1e161cb94082b9cff.png)
30
26
 
@@ -359,3 +355,11 @@
359
355
  {name: "FLUX"}])
360
356
 
361
357
  ```
358
+
359
+
360
+
361
+ 足りない情報があれば教えてください。
362
+
363
+
364
+
365
+ なんでカテゴリーが登録されないんでしょうか?

5

コード内容の修正

2020/08/30 11:03

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 新規投稿をするときに、カテゴリーデータベースに保存し投稿にカテゴリーのデータせたい。
3
+ 新規投稿をするときに、セレクトボックス用いて、カテゴリー機能実装したいです
4
-
5
- 現状は、投稿したときに、カテゴリー以外の情報はDBに登録される。
6
4
 
7
5
 
8
6
 
@@ -104,7 +102,7 @@
104
102
 
105
103
  .field-input
106
104
 
107
- = render partial: "category"
105
+ = f.select :category_ids, options_for_select( @category_parent_array.map{|c| [c, {}]}), {}, {class: "parent_select", id: "parent_category"}
108
106
 
109
107
  .actions
110
108
 
@@ -112,29 +110,145 @@
112
110
 
113
111
  ```
114
112
 
115
- _category.html.haml
116
-
117
- ```
118
-
119
- .categorySelect
120
-
121
- %select#parent_category
122
-
123
- - @parent_categories.each do |c|
124
-
125
- %option{value: "parent"}
126
-
127
- = c.name
128
-
129
-
130
-
131
- %select#children_category
132
-
133
- - @default_child_categories.each do |c|
134
-
135
- %option{value: "children"}
136
-
137
- = c.name
113
+ posts_cotllor.rb
114
+
115
+ ```
116
+
117
+ class PostsController < ApplicationController
118
+
119
+ before_action :authenticate_user!, except: [:index]
120
+
121
+ before_action :set_category, only:[:new, :create]
122
+
123
+
124
+
125
+ def index
126
+
127
+ @posts = Post.all
128
+
129
+ @category_parent_arrays = Category.where(ancestry: nil)
130
+
131
+ end
132
+
133
+
134
+
135
+ def show
136
+
137
+ @post = Post.find(params[:id])
138
+
139
+ @comment = Comment.new
140
+
141
+ @comments = @post.comments.includes(:user)
142
+
143
+ end
144
+
145
+
146
+
147
+ def new
148
+
149
+ @post = Post.new
150
+
151
+ end
152
+
153
+
154
+
155
+ def get_category_children
156
+
157
+ @category_children = Category.find("#{params[parent_id]}").children
158
+
159
+ end
160
+
161
+
162
+
163
+ def create
164
+
165
+ @post = Post.new(post_params)
166
+
167
+ @post.user_id = current_user.id
168
+
169
+ if @post.save
170
+
171
+ redirect_to post_path(@post), notice: '投稿されました'
172
+
173
+ else
174
+
175
+ render :new, alert: '投稿できませんでした'
176
+
177
+ end
178
+
179
+ end
180
+
181
+
182
+
183
+ def edit
184
+
185
+ @post = Post.find(params[:id])
186
+
187
+ if @post.user != current_user
188
+
189
+ redirect_to posts_path, alert: '不正なアクセスです'
190
+
191
+ end
192
+
193
+ end
194
+
195
+
196
+
197
+ def update
198
+
199
+ @post = Post.find(params[:id])
200
+
201
+ if @post.update(post_params)
202
+
203
+ redirect_to post_path(@post), notice: '投稿が更新されました'
204
+
205
+ else
206
+
207
+ render :edit
208
+
209
+ end
210
+
211
+ end
212
+
213
+
214
+
215
+ def destroy
216
+
217
+ post = Post.find(params[:id])
218
+
219
+ post.destroy
220
+
221
+ redirect_to posts_path
222
+
223
+ end
224
+
225
+
226
+
227
+ private
228
+
229
+ def post_params
230
+
231
+ params.require(:post).permit(:title, :body, :image)
232
+
233
+ end
234
+
235
+
236
+
237
+ def set_category
238
+
239
+ @category_parent_array = []
240
+
241
+ Category.where(ancestry: nil).each do |parent|
242
+
243
+ @category_parent_array << parent.name
244
+
245
+ end
246
+
247
+ end
248
+
249
+ end
250
+
251
+
138
252
 
139
253
  ```
140
254
 
@@ -218,298 +332,30 @@
218
332
 
219
333
  ```
220
334
 
221
- db/fixtures/01_category.rb
222
-
223
- ```
224
-
225
- parents = %w[ スノーボード ブーツ ビンディング ]
226
-
227
- snowboard_child = %w[ BURTON K2 SALOMON ]
228
-
229
- snowboots_child = %w[ BURTON DEELUXE FLUX ]
230
-
231
- binding_child = %w[ BURTON UNION FLUX ]
232
-
233
-
234
-
235
- parents.each do |parent|
236
-
237
- Category.seed do |c|
238
-
239
- c.name = parent
240
-
241
- end
242
-
243
- end
244
-
245
-
246
-
247
- snowboard_child.each do |child|
248
-
249
- Category.find_by!(name: 'スノーボード').children.create(name: child)
250
-
251
- end
252
-
253
-
254
-
255
- snowboots_child.each do |child|
256
-
257
- Category.find_by!(name: 'ブーツ').children.create(name: child)
258
-
259
- end
260
-
261
-
262
-
263
- binding_child.each do |child|
264
-
265
- Category.find_by!(name: 'ビンディング').children.create(name: child)
266
-
267
- end
268
-
269
- ```
270
-
271
- ##試したこと
272
-
273
- _form.html.hamlにて、以下のようにf.selectを使ってカテゴリーをデータベースに登録しようとしたが、
274
-
275
- category_idsの後に配列を組まなければならず、コードが冗長になってしまう。
276
-
277
-
278
-
279
- また、試しにこの方法で簡易的なデータを入力してデータベースに登録されているか確認したが、データベースにカテゴリーが登録されていなかった。
280
-
281
- ```
282
-
283
- .field
284
-
285
- .field-label
286
-
287
- = f.label :category, "カテゴリーを選択してください"
288
-
289
- .field-input
290
-
291
- = f.select :category_ids ##追記
292
-
293
- = render partial: "category"
294
-
295
- ```
296
-
297
- もしf.selectを使って、上記であげた部分テンプレートや01_category.rbにあるカテゴリー配列・定数を使ってデータベースに登録できる表記があればご教示ください。
298
-
299
- ※カテゴリーの配列は、この10倍ぐらいあります。なので、_form.html.hamlに直接書くのは好ましくないと思い、部分テンプレートなどを使っています。
300
-
301
- そのため、もし、f.select :category_idsの後に記述する方法があればお願いいたします。
302
-
303
-
304
-
305
- そのほかにも先輩方のカテゴリー登録できる方法があればご教示いただきたいです。
306
-
307
-
308
-
309
- 足りない情報があればお申し付けください。
310
-
311
-
312
-
313
- 【追記】
314
-
315
- @parent_categories. @default_child_categoriesの変数について
316
-
317
-
318
-
319
- posts_cotroller.rb
320
-
321
- ```
322
-
323
- class PostsController < ApplicationController
324
-
325
- before_action :authenticate_user!, except: [:index]
326
-
327
- before_action :set_categories, only: [:new, :create]
328
-
329
- def index
330
-
331
- @posts = Post.all
332
-
333
- end
334
-
335
-
336
-
337
- def show
338
-
339
- @post = Post.find(params[:id])
340
-
341
- @comment = Comment.new
342
-
343
- @comments = @post.comments.includes(:user)
344
-
345
- end
346
-
347
-
348
-
349
- def new
350
-
351
- @post = Post.new
352
-
353
- end
354
-
355
-
356
-
357
- def create
358
-
359
- @post = Post.new(post_params)
360
-
361
- @post.user_id = current_user.id
362
-
363
- if @post.save
364
-
365
- redirect_to post_path(@post), notice: '投稿されました'
366
-
367
- else
368
-
369
- render :new, alert: '投稿できませんでした'
370
-
371
- end
372
-
373
- end
374
-
375
-
376
-
377
- def edit
378
-
379
- @post = Post.find(params[:id])
380
-
381
- if @post.user != current_user
382
-
383
- redirect_to posts_path, alert: '不正なアクセスです'
384
-
385
- end
386
-
387
- end
388
-
389
-
390
-
391
- def update
392
-
393
- @post = Post.find(params[:id])
394
-
395
- if @post.update(post_params)
396
-
397
- redirect_to post_path(@post), notice: '投稿が更新されました'
398
-
399
- else
400
-
401
- render :edit
402
-
403
- end
404
-
405
- end
406
-
407
-
408
-
409
- def destroy
410
-
411
- post = Post.find(params[:id])
412
-
413
- post.destroy
414
-
415
- redirect_to posts_path
416
-
417
- end
418
-
419
-
420
-
421
- def dynamic_select_category
422
-
423
- @category = Category.find(params[:category_id])
424
-
425
- end
426
-
427
-
428
-
429
- private
430
-
431
- def post_params
432
-
433
- params.require(:post).permit(:title, :body, :image)
434
-
435
- end
436
-
437
-
438
-
439
- def set_categories
440
-
441
- @category_pararent = Category.where(ancestry: nil)
442
-
443
- end
444
-
445
-
446
-
447
- end
448
-
449
-
450
-
451
- ```
452
-
453
-
454
-
455
- ```
456
-
457
- = form_for(@post) do |f|
458
-
459
- .field
460
-
461
- .field-label
462
-
463
- = f.label :title, "snowB"
464
-
465
- .field-input
466
-
467
- = f.text_field :title
468
-
469
- .field
470
-
471
- .field-label
472
-
473
- = f.label :body, "ココがポイント!"
474
-
475
- .field-input
476
-
477
- = f.text_area :body
478
-
479
- .field
480
-
481
- .field-label
482
-
483
- = f.label :image, "snowB写真"
484
-
485
- .field-input
486
-
487
- = f.attachment_field :image
488
-
489
- .field
490
-
491
- .field-label
492
-
493
- = f.label :category, "カテゴリーを選択してください"
494
-
495
- .field-input
496
-
497
- = f.fields_for :categories do |c|
498
-
499
- = c.select :category, options_for_select(@category_parent.map{|c| [c[:name], c[:id]]}), {class: "parant_category_box", id: "parent_category"}
500
-
501
- .actions
502
-
503
- = f.submit "投稿する", class: "btn"
504
-
505
- ```
506
-
507
-
508
-
509
- 参考記事:
510
-
511
- https://qiita.com/manbolila/items/7c44142de50093470580
512
-
513
-
514
-
515
- ![イメージ説明](864e6e09ea2036911b7422c4772bfaab.png)
335
+ db/seeds.rb
336
+
337
+ ```
338
+
339
+ snowboard = Category.create(name: "スノーボード")
340
+
341
+ snowboard_1 = snowboard.children.create([
342
+
343
+ {name: "BURTON"},
344
+
345
+ {name: "K2"},
346
+
347
+ {name: "SALOMON"}])
348
+
349
+
350
+
351
+ snowboots = Category.create(name: "ブーツ")
352
+
353
+ snowboots_1 = snowboots.children.create([
354
+
355
+ {name: "BURTON"},
356
+
357
+ {name: "DEELUXE"},
358
+
359
+ {name: "FLUX"}])
360
+
361
+ ```

4

タイトルの変更

2020/08/30 10:57

投稿

susume
susume

スコア13

test CHANGED
@@ -1 +1 @@
1
- カテゴリーをDBに登録し、投稿にカテゴリーデータせた
1
+ 新規投稿にセレクトボックスを用いて、カテゴリー機能実装したい
test CHANGED
File without changes

3

options_forなどを記載

2020/08/30 10:53

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -324,7 +324,7 @@
324
324
 
325
325
  before_action :authenticate_user!, except: [:index]
326
326
 
327
- before_action :set_categories, only: [:edit, :new]
327
+ before_action :set_categories, only: [:new, :create]
328
328
 
329
329
  def index
330
330
 
@@ -438,18 +438,78 @@
438
438
 
439
439
  def set_categories
440
440
 
441
- @parent_categories = Category.where(ancestry: nil)
441
+ @category_pararent = Category.where(ancestry: nil)
442
-
443
- @default_child_categories = @parent_categories.first.children
442
+
444
-
445
- end
443
+ end
446
-
447
-
448
-
444
+
445
+
446
+
449
- end
447
+ end
450
-
451
-
452
-
448
+
449
+
450
+
453
- ```
451
+ ```
454
-
452
+
453
+
454
+
455
- a
455
+ ```
456
+
457
+ = form_for(@post) do |f|
458
+
459
+ .field
460
+
461
+ .field-label
462
+
463
+ = f.label :title, "snowB"
464
+
465
+ .field-input
466
+
467
+ = f.text_field :title
468
+
469
+ .field
470
+
471
+ .field-label
472
+
473
+ = f.label :body, "ココがポイント!"
474
+
475
+ .field-input
476
+
477
+ = f.text_area :body
478
+
479
+ .field
480
+
481
+ .field-label
482
+
483
+ = f.label :image, "snowB写真"
484
+
485
+ .field-input
486
+
487
+ = f.attachment_field :image
488
+
489
+ .field
490
+
491
+ .field-label
492
+
493
+ = f.label :category, "カテゴリーを選択してください"
494
+
495
+ .field-input
496
+
497
+ = f.fields_for :categories do |c|
498
+
499
+ = c.select :category, options_for_select(@category_parent.map{|c| [c[:name], c[:id]]}), {class: "parant_category_box", id: "parent_category"}
500
+
501
+ .actions
502
+
503
+ = f.submit "投稿する", class: "btn"
504
+
505
+ ```
506
+
507
+
508
+
509
+ 参考記事:
510
+
511
+ https://qiita.com/manbolila/items/7c44142de50093470580
512
+
513
+
514
+
515
+ ![イメージ説明](864e6e09ea2036911b7422c4772bfaab.png)

2

初心者マークの追加

2020/08/30 07:31

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -451,3 +451,5 @@
451
451
 
452
452
 
453
453
  ```
454
+
455
+ a

1

コントローラーの追記

2020/08/30 04:08

投稿

susume
susume

スコア13

test CHANGED
File without changes
test CHANGED
@@ -307,3 +307,147 @@
307
307
 
308
308
 
309
309
  足りない情報があればお申し付けください。
310
+
311
+
312
+
313
+ 【追記】
314
+
315
+ @parent_categories. @default_child_categoriesの変数について
316
+
317
+
318
+
319
+ posts_cotroller.rb
320
+
321
+ ```
322
+
323
+ class PostsController < ApplicationController
324
+
325
+ before_action :authenticate_user!, except: [:index]
326
+
327
+ before_action :set_categories, only: [:edit, :new]
328
+
329
+ def index
330
+
331
+ @posts = Post.all
332
+
333
+ end
334
+
335
+
336
+
337
+ def show
338
+
339
+ @post = Post.find(params[:id])
340
+
341
+ @comment = Comment.new
342
+
343
+ @comments = @post.comments.includes(:user)
344
+
345
+ end
346
+
347
+
348
+
349
+ def new
350
+
351
+ @post = Post.new
352
+
353
+ end
354
+
355
+
356
+
357
+ def create
358
+
359
+ @post = Post.new(post_params)
360
+
361
+ @post.user_id = current_user.id
362
+
363
+ if @post.save
364
+
365
+ redirect_to post_path(@post), notice: '投稿されました'
366
+
367
+ else
368
+
369
+ render :new, alert: '投稿できませんでした'
370
+
371
+ end
372
+
373
+ end
374
+
375
+
376
+
377
+ def edit
378
+
379
+ @post = Post.find(params[:id])
380
+
381
+ if @post.user != current_user
382
+
383
+ redirect_to posts_path, alert: '不正なアクセスです'
384
+
385
+ end
386
+
387
+ end
388
+
389
+
390
+
391
+ def update
392
+
393
+ @post = Post.find(params[:id])
394
+
395
+ if @post.update(post_params)
396
+
397
+ redirect_to post_path(@post), notice: '投稿が更新されました'
398
+
399
+ else
400
+
401
+ render :edit
402
+
403
+ end
404
+
405
+ end
406
+
407
+
408
+
409
+ def destroy
410
+
411
+ post = Post.find(params[:id])
412
+
413
+ post.destroy
414
+
415
+ redirect_to posts_path
416
+
417
+ end
418
+
419
+
420
+
421
+ def dynamic_select_category
422
+
423
+ @category = Category.find(params[:category_id])
424
+
425
+ end
426
+
427
+
428
+
429
+ private
430
+
431
+ def post_params
432
+
433
+ params.require(:post).permit(:title, :body, :image)
434
+
435
+ end
436
+
437
+
438
+
439
+ def set_categories
440
+
441
+ @parent_categories = Category.where(ancestry: nil)
442
+
443
+ @default_child_categories = @parent_categories.first.children
444
+
445
+ end
446
+
447
+
448
+
449
+ end
450
+
451
+
452
+
453
+ ```