何度もすみませんが、まったくできずに本当に困っています。。
進めようがなく、死んでますのでどうかお助けくださいませ。
##実現したいこと・前提
Ruby on Railsで食べログのような口コミサイトを作っています。
CarrierWaveを利用して複数の画像を、Restaurantモデルに紐付けて、複数枚表示したいと考えています。
そのために、こちらのリンクを参考にして、Albumモデルを作成し、その下に入れ子としてAlbum_contentsモデルを作成することになりました。
現状の課題は3点あります。
・Album_contents内で複数画像がアップロードできていない
→ アップロードしようとすると全て一枚の画像が表示されてしまいます
・RestaurantとAlbum、Album_contentsが紐付いていないと思われる
・viewに表示する際の文法が分からない
(@restaurant.album.album_contents.image_1となるのでしょうか??)
すみませんが、何卒よろしくお願い申し上げます。
##開発環境
Ruby on Rails 5.0.0.1
CarrierWave
ActiveAdmin
###該当するソースコード
restaurantモデル(restaurant.rb)
Ruby
1class Restaurant < ApplicationRecord 2 has_many :reviews 3 belongs_to :area 4 belongs_to :album 5 mount_uploader :image, ImageUploader 6 geocoded_by :mapaddress 7 after_validation :geocode 8end
albumモデル(album.rb)
Ruby
1class Album < ApplicationRecord 2 belongs_to :restaurant 3 has_many :album_contents 4 accepts_nested_attributes_for :album_contents 5end
album_contentsモデル(album_contents.rb)
Ruby
1class AlbumContent < ApplicationRecord 2 belongs_to :album 3 mount_uploader :image_1, ImageUploader 4 mount_uploader :image_2, ImageUploader 5 mount_uploader :image_3, ImageUploader 6 mount_uploader :image_4, ImageUploader 7 mount_uploader :image_5, ImageUploader 8 mount_uploader :image_6, ImageUploader 9 mount_uploader :image_7, ImageUploader 10 mount_uploader :image_8, ImageUploader 11 mount_uploader :image_9, ImageUploader 12end
albumsコントローラー(albums_controllers.rb)
Ruby
1class AlbumsController < ApplicationController 2 def new 3 @album = Album.new(create_params) 4 9.times { @album.album_contents.build } 5 end 6 7 private 8 9 def create_params 10 params.require(:album).permit(album_contents_attributes: [:image_1], 11 album_contents_attributes: [:image_2], album_contents_attributes: [:image_3], 12 album_contents_attributes: [:image_4], album_contents_attributes: [:image_5], 13 album_contents_attributes: [:image_6], album_contents_attributes: [:image_7], 14 album_contents_attributes: [:image_8], album_contents_attributes: [:image_9]) 15 end 16end
##エラーメッセージ
Ruby
1NoMethodError in Admin::Albums#new
本当に困っています、
すみませんが、どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。