1

我想知道如何为多态关联准备工作表单(使用 simple_form)。使用这些模型:

class Poster < ActiveRecord::Base
  belongs_to :kind, polymorphic: true      
end

class Category < ActiveRecord::Base
  has_many :posters, as: :kind
  has_many :subcategories, dependent: :destroy
  accepts_nested_attributes_for :subcategories

  def self.with_subcategories
    self.all.map { |category| category.subcategories }
  end  
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :posters, as: :kind
end

我希望用户能够创建海报,并将其分配给现有类别或子类别之一。在数据库(海报)中,我有 kind_id 和 kind_type,但我不知道如何构建正确的表单。我尝试了这样的事情(仅使用子类别进行测试),但它甚至不接近正确:

= simple_form_for @poster do |f|
    = f.input :description
    = f.input :title
    = f.input :kind, collection: Category.with_subcategories
    = f.submit

有关如何正确执行此操作的任何线索?

4

0 回答 0