質問編集履歴

2

追加でエラーが発生してしまいました

2017/05/19 11:30

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -359,3 +359,349 @@
359
359
  end
360
360
 
361
361
  ```
362
+
363
+
364
+
365
+ ###追記でエラーが発生しました
366
+
367
+
368
+
369
+ いただいた通り、albumの下に入れ子を作ってみるとかなり複雑っぽくなってしまい、多々エラーが発生してしまいました。。泣
370
+
371
+ まず、直したいところは下記2点です。
372
+
373
+
374
+
375
+ ・active admin上で、アルバムとレストランを紐付けたい
376
+
377
+ 現在はアルバムを選択する項目が消えてしまいました。。下記はレストランを入力するフォームです。
378
+
379
+ ![イメージ説明](a0fb4e8ea9ad20734d55b71348b78d56.png)
380
+
381
+
382
+
383
+ active_admin側はgemで
384
+
385
+
386
+
387
+ ・アルバムの画像を表示させるエラーが出てしまいます
388
+
389
+ NoMethodErrorとなってしまい、ビューの書き方に問題があるかと考えられます。
390
+
391
+
392
+
393
+ ・active admin上でアルバムを入力するビューになぜか、アルバムIDが不明に2つ出ています。
394
+
395
+ ![イメージ説明](5ebb4743cd51ca453acfb3aeb05915e6.png)
396
+
397
+
398
+
399
+
400
+
401
+ ビュー
402
+
403
+ ```Ruby
404
+
405
+ <!-- Wrapper for slides -->
406
+
407
+ <div class="carousel-inner" role="listbox">
408
+
409
+ <div class="item active">
410
+
411
+ <%= image_tag @restaurant.image %>
412
+
413
+ </div>
414
+
415
+ <div class="item restaurant-image">
416
+
417
+ <%= image_tag @restaurant.album.image_1 %>
418
+
419
+ </div>
420
+
421
+ <div class="item restaurant-image">
422
+
423
+ <%= image_tag @restaurant.album.image_2 %>
424
+
425
+ </div>
426
+
427
+ <div class="item restaurant-image">
428
+
429
+ <%= image_tag @restaurant.album.image_3 %>
430
+
431
+ </div>
432
+
433
+ <div class="item restaurant-image">
434
+
435
+ <%= image_tag @restaurant.album.image_4 %>
436
+
437
+ </div>
438
+
439
+ <div class="item restaurant-image">
440
+
441
+ <%= image_tag @restaurant.album.image_5 %>
442
+
443
+ </div>
444
+
445
+ <div class="item restaurant-image">
446
+
447
+ <%= image_tag @restaurant.album.image_6 %>
448
+
449
+ </div>
450
+
451
+ </div>
452
+
453
+ ```
454
+
455
+
456
+
457
+ **■ レストラン側のコード**
458
+
459
+
460
+
461
+ ・レストランのモデル
462
+
463
+ ```Ruby
464
+
465
+ class Restaurant < ApplicationRecord
466
+
467
+ belongs_to :area
468
+
469
+ has_many :album
470
+
471
+ mount_uploader :image, ImageUploader
472
+
473
+ geocoded_by :mapaddress
474
+
475
+ after_validation :geocode
476
+
477
+ end
478
+
479
+ ```
480
+
481
+ ・レストランのコントローラー(restaurant_controllers.rb)
482
+
483
+ ```Ruby
484
+
485
+ class RestaurantsController < ApplicationController
486
+
487
+ ・・・
488
+
489
+ def restaurant_params
490
+
491
+ params.require(:restaurant).permit(:id, :name, :image, :genre, :access, :hour,
492
+
493
+ :address, :phone, :website,
494
+
495
+ :description, :seats, :area_id, :album_id, :mapaddress, :latitude, :longitude, albums_attributes: [:image])
496
+
497
+ end
498
+
499
+ ・・・
500
+
501
+ end
502
+
503
+ ```
504
+
505
+ ・レストランとアルバムを紐付けるマイグレーションファイル
506
+
507
+ 日付_album_id_to_restaurants.rb
508
+
509
+ ```Ruby
510
+
511
+ class AlbumIdToRestaurants < ActiveRecord::Migration[5.0]
512
+
513
+ def self.up
514
+
515
+ add_column :restaurants, :album_id, :integer
516
+
517
+ add_index :restaurants, :album_id
518
+
519
+ end
520
+
521
+
522
+
523
+ def self.down
524
+
525
+ remove_index :restaurants, :column => :album_id
526
+
527
+ remove_column :restaurants, :album_id
528
+
529
+ end
530
+
531
+ end
532
+
533
+ ```
534
+
535
+
536
+
537
+ **■ アルバム側のコード**
538
+
539
+ ・アルバムのモデル
540
+
541
+ ```Ruby
542
+
543
+ class Album < ApplicationRecord
544
+
545
+ belongs_to :restaurant
546
+
547
+ has_many :album_contents
548
+
549
+ accepts_nested_attributes_for :album_contents
550
+
551
+ end
552
+
553
+ ```
554
+
555
+
556
+
557
+ ・アルバムのコントローラー
558
+
559
+ ```Ruby
560
+
561
+ class AlbumController < ApplicationController
562
+
563
+ def new
564
+
565
+ @album = Album.new(create_params)
566
+
567
+ 9.times { @album.album_contents.build }
568
+
569
+ end
570
+
571
+
572
+
573
+ private
574
+
575
+
576
+
577
+ def create_params
578
+
579
+ params.require(:album).permit(album_contents_attributes: [:image])
580
+
581
+ end
582
+
583
+ end
584
+
585
+ ```
586
+
587
+ ・アルバムのマイグレーションファイル
588
+
589
+ ```Ruby
590
+
591
+ class CreateAlbums < ActiveRecord::Migration[5.0]
592
+
593
+ def change
594
+
595
+ create_table :albums do |t|
596
+
597
+ t.integer :restaurant_id
598
+
599
+
600
+
601
+ t.timestamps
602
+
603
+ end
604
+
605
+ end
606
+
607
+ end
608
+
609
+ ```
610
+
611
+ ・アルバムの入れ子となるアルバムコンテンツのモデル
612
+
613
+ ```Ruby
614
+
615
+ class AlbumContent < ApplicationRecord
616
+
617
+ belongs_to :album
618
+
619
+ mount_uploader :image_1, AlbumContentUploader
620
+
621
+ mount_uploader :image_2, AlbumContentUploader
622
+
623
+ mount_uploader :image_3, AlbumContentUploader
624
+
625
+ mount_uploader :image_4, AlbumContentUploader
626
+
627
+ mount_uploader :image_5, AlbumContentUploader
628
+
629
+ mount_uploader :image_6, AlbumContentUploader
630
+
631
+ mount_uploader :image_7, AlbumContentUploader
632
+
633
+ mount_uploader :image_8, AlbumContentUploader
634
+
635
+ mount_uploader :image_9, AlbumContentUploader
636
+
637
+ end
638
+
639
+ ```
640
+
641
+
642
+
643
+ ・アルバムコンテンツのマイグレーションファイル
644
+
645
+ ```Ruby
646
+
647
+ class CreateAlbumContents < ActiveRecord::Migration[5.0]
648
+
649
+ def change
650
+
651
+ create_table :album_contents do |t|
652
+
653
+ t.integer :album_id
654
+
655
+ t.string :image_1
656
+
657
+ t.string :image_2
658
+
659
+ t.string :image_3
660
+
661
+ t.string :image_4
662
+
663
+ t.string :image_5
664
+
665
+ t.string :image_6
666
+
667
+ t.string :image_7
668
+
669
+ t.string :image_8
670
+
671
+ t.string :image_9
672
+
673
+
674
+
675
+ t.timestamps
676
+
677
+ end
678
+
679
+ end
680
+
681
+ end
682
+
683
+ ```
684
+
685
+
686
+
687
+ ・アルバムとアルバムコンテンツを紐付けるマイグレーションファイル
688
+
689
+ ```Ruby
690
+
691
+ class AddAlbumRefToAlbumContent < ActiveRecord::Migration[5.0]
692
+
693
+ def change
694
+
695
+ add_column :album_contents, :album, :refernces
696
+
697
+ end
698
+
699
+ end
700
+
701
+ ```
702
+
703
+
704
+
705
+ いろいろ反省しかありません。。
706
+
707
+ すみませんが、おたすけくださいませ。

1

Railsのバージョンを間違えました

2017/05/19 11:30

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- 開発環境:Ruby on Rails 5.0.0.2
13
+ 開発環境:Ruby on Rails 5.0.0.1
14
14
 
15
15
 
16
16