ActiveAdmin 嵌套表单不起作用

ActiveAdmin nested form doesn't work

我在 rails application.My 中使用 ActiveAdmin,设置如下:

app/models/translation_type.rb

class TranslationType < ActiveRecord::Base
    has_many :translation_details
    accepts_nested_attributes_for :translation_details, allow_destroy: true
end

app/models/translation_detail.rb

class TranslationDetail < ActiveRecord::Base
  belongs_to :translation_type
  attr_accessor :id, :language_from, :language_to, :price
end

app/admin/translation_type.rb

ActiveAdmin.register TranslationType do
  permit_params :translation_type, translation_details_attributes: [:id, :language_from, :language_to, :price, :_destroy]

  index do
    column :translation_type
    column :translation_details
    column :created_at
    column :updated_at
    actions
  end

  filter :translation_type
  filter :created_at

  form do |f|
    f.inputs "Translation Type Details" do
      f.input :translation_type
      f.input :created_at
      f.input :updated_at
    end
    f.inputs do
      f.has_many :translation_details, allow_destroy: true do |tran_det|
        tran_det.input :language_from, :collection => ['Gr', 'En', 'Fr', 'De']
        tran_det.input :language_to, :collection => ['Gr', 'En', 'Fr', 'De']
        tran_det.input :price
      end
    end
    f.actions
  end

  controller do
    before_filter { @page_title = :translation_type } 
  end

我不需要 translation_detail 的部分,所以我省略了 app/admin/translation_detail.rb

我想在我的 translation_type 表单中包含 translation_detail 的嵌套形式。使用上面的代码创建嵌套表单,但提交后 translation_detail 的属性未保存。此外,当我尝试更新或删除 translation_detail 时,会显示此消息

Couldn't find TranslationDetail with ID=* for TranslationType with ID=*

如何解决这个问题?

好像是这一行

attr_accessor :id, :language_from, :language_to, :price

app/models/translation_detail.rb 中导致错误,但我不太确定原因。

我 运行 在保存以前未保存且数据库中不存在的字段时遇到同样的问题。我不得不从模型文件中删除 attr_accessor :fieldName 以使其永久化。

如此 answers 所述:Rails 上的 attr_accessor 仅在您不希望将属性保存到数据库时使用,仅作为该模型的一个实例。

问题示例中可能发生的事情是它无法保存 TranslationDetail,因为除了它与 TranslationType 的关系之外的所有内容在保存之前都被丢弃了。