I am trying setup a select menu to associate a ImageGallery with a product. The ImageGallery is polymorphic since it is shared among a few models. Formtastic seems to be very confused about what to do. It is trying to call a method called Galleryable, the name of my polymorphic association, _id (galleryable_id) on the product model.
Product
class Product < ActiveRecord::Base
has_one :image_gallery, as: :galleryable, dependent: :destroy
accepts_nested_attributes_for :image_gallery, :allow_destroy => true
end
Gallery
class ImageGallery < ActiveRecord::Base
belongs_to :galleryable, polymorphic: true
validates :title, presence: true
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images, :allow_destroy => true, reject_if: lambda { |t| t['file'].nil? }
end
Active Admin form
form do |f|
f.inputs "Details" do
f.input :name
f.input :category
f.input :price
f.input :purchase_path
f.input :video_panels
f.input :image_panels
f.input :image_gallery, :as => :select, collection: ImageGallery.all, value_method: :id
end
f.inputs "Image", :for => [:image, f.object.image || Image.new] do |i|
i.input :title
i.input :file, :as => :file, required: false, :hint => i.template.image_tag(i.object.file.url(:thumb))
end
f.actions
end
I played with defining galleryable_id on the model, but this tries to update the product with the attribute which of course does not exist.
Has anyone successfully set this up?
Thanks,
Cory