4

ImageGallery를 제품과 연결하기위한 선택 메뉴를 설치하려고합니다. ImageGallery는 여러 모델에서 공유되므로 다형성을 갖습니다. Formtastic은해야 할 일에 대해 매우 혼란스러워합니다. 그것은 제품 모델에서 _id (galleryable_id)라는 다형성 연관의 이름 인 Galleryable이라는 메소드를 호출하려고합니다.활성 관리자가 하나의 다형성 양식을 가지고 있습니다.

제품

class Product < ActiveRecord::Base 
    has_one :image_gallery, as: :galleryable, dependent: :destroy 
    accepts_nested_attributes_for :image_gallery, :allow_destroy => true 
end 

갤러리

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 

내가 모델에 galleryable_id을 정의와 함께 연주하지만,이 속성을 가진 제품을 업데이트하려고

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 

액티브 관리 양식하는 물론 존재하지 않습니다.

누구나 성공적으로 설정 했습니까?

감사합니다,

코리

답변

1

나는 꽤 흥미로운 시나리오 때문에 아무도 대답하지 놀랐어요.

당신은 거의 가지고 있지만 당신의 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).

도움이 되었기를 바랍니다.