我有一個模型'畫廊',它與'Exhbition Images'(並與belongs_to相反)具有'has_many'關係。訪問屬於另一個模型的模型
我想通過圖庫模型顯示與每個圖庫相關的圖像。但是我遇到的錯誤
undefined method `exhibition_images' for #<Gallery::ActiveRecord_Relation:0x00000108159dd0>
當我試圖實現這行代碼
gallery index:
<%= @gallery.exhibition_images.each do |exhibition_image| %>
<%= exhibition_image.title %>
<% end %>
我相信這是很簡單的,我失去了一些東西明顯。
畫廊控制器
def index
@gallery = Gallery.all
end
畫廊模式
class Gallery < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :guide
has_many :exhibition_images
accepts_nested_attributes_for :exhibition_images
end
展覽圖片
class ExhibitionImage < ActiveRecord::Base
belongs_to :gallery
has_attached_file :image, styles: { small: "100x100", guide: "500x500" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
SCHEMA
create_table "exhibition_images", force: true do |t|
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "gallery_id"
t.datetime "created_at"
t.datetime "updated_at"
末
create_table "galleries", force: true do |t|
t.string "title"
t.string "slug"
t.integer "guide_id"
t.datetime "created_at"
t.datetime "updated_at"
end