The active administrator has one polymorphic form

I am trying to customize a selection menu to associate ImageGallery with a product. ImageGallery is polymorphic because it is shared among several models. Formtastic seems to be very confused about what to do. He tries to call a method called Galleryable, the name of my polymorphic association, _id (galleryable_id) in 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 the definition of galleryable_id on the model, but this is trying to update the product with an attribute that, of course, does not exist.

Has anyone successfully installed this?

Thank,

Corey

+4
source share
1 answer

, , .

, AA. :

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.inputs "ImageGallery", :for => [:image_gallery, f.object.image_gallery || ImageGallery.new] do |gallery|
      gallery.has_many :images do |image|
        image.input :title
        image.input :file, :as => :file, required: false, :hint => image.template.image_tag(image.object.file.url(:thumb))
      end
    end
  end
  f.actions
end

"ImageGallery" . has_one ( f.input :image_gallery).

, :)

+1

Source: https://habr.com/ru/post/1533987/


All Articles