6

我有两个模型

class Entity < ActiveRecord::Base
  # Associations
  has_many :contacts
  accepts_nested_attributes_for :contacts, :allow_destroy => true
end

class Contact < ActiveRecord::Base
  # Associations
  belongs_to :entity
end

现在在 rails admin 我得到以下选项。

添加新的联系表

在此处输入图像描述


添加新的实体表单

在此处输入图像描述

我需要在联系表单中隐藏实体字段,同时添加新实体。

任何帮助都会很有用。

4

2 回答 2

12

您可以像这样使用 inverse_of 自动隐藏字段

class Entity < ActiveRecord::Base
  # Associations
  has_many :contacts, inverse_of: :entity
  accepts_nested_attributes_for :contacts, allow_destroy: true
end

class Contact < ActiveRecord::Base
  # Associations
  belongs_to :entity, inverse_of: :contacts
end

如果在关系上设置 :inverse_of 选项,RailsAdmin 将自动在模态创建窗口中填充反向关系。(链接旁边的 :belongs_to 和 :has_many 多选小部件)

来源:https ://github.com/sferik/rails_admin/wiki/Associations-basics

让我知道进展如何

于 2013-11-26T18:12:21.550 回答
6

为了完整起见,因为我也遇到了这个问题并解决了它,如果你愿意,你可以在嵌套表单中使用模型时配置模型,就像编辑、更新、创建和嵌套一样

class Contact < ActiveRecord::Base
  # Associations
  belongs_to :entity

  rails_admin do
    nested do
      configure :entity do
        hide
      end
    end
  end

end

访问官方 wiki了解更多信息

于 2014-02-14T23:11:01.113 回答