委托方法的模型的 ActiveAdmin 未知属性 'attribute'

ActiveAdmin unknown attribute 'attribute' for Model for delegated method

尽管委托了属性,但在尝试通过 ActiveAdmin 保存模型时,我收到了 OP 标题中提到的错误。 具体来说,是 ActiveRecord 引发了此处发生的错误:

activerecord (4.2.7.1) lib/active_record/attribute_assignment.rb:59:in `rescue in _assign_attribute'

我的模型是:

class Hero  < ActiveRecord::Base
  has_one :link, as: :linkable
  delegate :url, :section_id, :pdf, :path, to: :link, allow_nil: true
  accepts_nested_attributes_for :link, allow_destroy: true
end

而且 Link 在模型上肯定有 section_id,正如我在架构中看到的那样

问题是当你使用委托时,它不会委托属性而是委托方法(如果我错了请纠正我),这意味着你必须这样做:

class Hero  < ActiveRecord::Base
  has_one :link, as: :linkable
  delegate :url, :section_id,:section_id=, :pdf, :path, to: :link, allow_nil: true
  accepts_nested_attributes_for :link, allow_destroy: true
end

这对我有用。