下記のURLを参考にして、Carrierwaveでアップロードした複数の写真を特定のモデル(restaurant)に紐付かせたいのですが、できなくて困っています。
http://qiita.com/sinagaki58/items/a0d59cc41c6824bb5f67
restaurantモデルに対して、URL中のprototypeにあたるalbumモデルと、入れ子のalbum_contentを用意しています。
開発環境:Ruby on Rails 5.0.0.1
###エラーメッセージ
No Method Error
・ビュー
Ruby
1 <!-- Wrapper for slides --> 2 <div class="carousel-inner" role="listbox"> 3 <div class="item active"> 4 <%= image_tag @restaurant.image %> 5 </div> 6 <div class="item restaurant-image"> 7 <%= image_tag @restaurant.album.image_1 %> 8 </div> 9 <div class="item restaurant-image"> 10 <%= image_tag @restaurant.album.image_2 %> 11 </div> 12 <div class="item restaurant-image"> 13 <%= image_tag @restaurant.album.image_3 %> 14 </div> 15 <div class="item restaurant-image"> 16 <%= image_tag @restaurant.album.image_4 %> 17 </div> 18 <div class="item restaurant-image"> 19 <%= image_tag @restaurant.album.image_5 %> 20 </div> 21 <div class="item restaurant-image"> 22 <%= image_tag @restaurant.album.image_6 %> 23 </div> 24 </div>
###該当するソースコード
■ レストラン側のコード
・レストランのモデル
Ruby
1class Restaurant < ApplicationRecord 2 belongs_to :area 3 has_many :album 4 mount_uploader :image, ImageUploader 5 geocoded_by :mapaddress 6 after_validation :geocode 7end
・レストランのコントローラー(restaurant_controllers.rb)
Ruby
1class RestaurantsController < ApplicationController 2・・・ 3 def restaurant_params 4 params.require(:restaurant).permit(:id, :name, :image, :genre, :access, :hour, 5 :address, :phone, :website, 6 :description, :seats, :area_id, :album_id, :mapaddress, :latitude, :longitude, albums_attributes: [:image]) 7 end 8・・・ 9end
・レストランとアルバムを紐付けるマイグレーションファイル
日付_album_id_to_restaurants.rb
Ruby
1class AlbumIdToRestaurants < ActiveRecord::Migration[5.0] 2 def self.up 3 add_column :restaurants, :album_id, :integer 4 add_index :restaurants, :album_id 5 end 6 7 def self.down 8 remove_index :restaurants, :column => :album_id 9 remove_column :restaurants, :album_id 10 end 11end
■ アルバム側のコード
・アルバムのモデル
Ruby
1class Album < ApplicationRecord 2 belongs_to :restaurant 3 has_many :album_contents 4 accepts_nested_attributes_for :album_contents 5end
・アルバムのコントローラー
Ruby
1class AlbumController < 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]) 11 end 12end
・アルバムのマイグレーションファイル
Ruby
1class CreateAlbums < ActiveRecord::Migration[5.0] 2 def change 3 create_table :albums do |t| 4 t.integer :restaurant_id 5 6 t.timestamps 7 end 8 end 9end
・アルバムの入れ子となるアルバムコンテンツのモデル
Ruby
1class AlbumContent < ApplicationRecord 2 belongs_to :album 3 mount_uploader :image_1, AlbumContentUploader 4 mount_uploader :image_2, AlbumContentUploader 5 mount_uploader :image_3, AlbumContentUploader 6 mount_uploader :image_4, AlbumContentUploader 7 mount_uploader :image_5, AlbumContentUploader 8 mount_uploader :image_6, AlbumContentUploader 9 mount_uploader :image_7, AlbumContentUploader 10 mount_uploader :image_8, AlbumContentUploader 11 mount_uploader :image_9, AlbumContentUploader 12end
・アルバムコンテンツのマイグレーションファイル
Ruby
1class CreateAlbumContents < ActiveRecord::Migration[5.0] 2 def change 3 create_table :album_contents do |t| 4 t.integer :album_id 5 t.string :image_1 6 t.string :image_2 7 t.string :image_3 8 t.string :image_4 9 t.string :image_5 10 t.string :image_6 11 t.string :image_7 12 t.string :image_8 13 t.string :image_9 14 15 t.timestamps 16 end 17 end 18end
・アルバムとアルバムコンテンツを紐付けるマイグレーションファイル
Ruby
1class AddAlbumRefToAlbumContent < ActiveRecord::Migration[5.0] 2 def change 3 add_column :album_contents, :album, :refernces 4 end 5end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。