0

我有一个模型Medium,它通过 STI 有两个子类

class Medium < ApplicationRecord
end

class Image < Medium
end

class Video < Medium
end

这两个子类可以通过多对多关系与 a 的实例相关联Brand。联合表称为Ìllustrations

class Brand < ApplicationRecord
  has_many :illustrations, as: :illustrateable, dependent: :destroy
  has_many :images, through: :illustrations, source: :illustrateable, source_type: 'Image'  
  has_many :videos, through: :illustrations, source: :illustrateable, source_type: 'Video'
end

class Illustration < ApplicationRecord
  belongs_to :illustrateable, polymorphic: true
  belongs_to :brand
end

当我想使用 Administrate gem 在联合表中创建记录时,界面看起来非常整洁。

在此处输入图像描述

这使用以下管理代码:

class IllustrationDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    illustrateable: Field::Polymorphic.with_options(
      classes: [Image, Video]
    ),
    brand: Field::BelongsTo,
    id: Field::String,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze
end

我遇到的问题是 Administrate 总是保存illustrateable_type: "Medium", notImageVideo. 我怎样才能解决这个问题?下面是参数的样子:

#<ActionController::Parameters {"authenticity_token"=>"ySVJJ5f6lnWO97UNE1Fh8k7mfKu57pu8X1FHL0xgraY3WwvFqTSTNAxBR8AQ4TGzxZMjHIj8St1AJkbzcBWsSg", "illustration"=>{"illustrateable"=>{"type"=>"Administrate::Field::Polymorphic", "value"=>"gid://novoglobo/Image/c35498dc-8209-419b-a43d-5ca26bc11beb"}, "brand_id"=>"3bef71ad-c41a-4b96-9a91-6e6db847009c"}, "commit"=>"Create Illustration", "controller"=>"admin/illustrations", "action"=>"create"} permitted: false>
4

0 回答 0