ruby-on-rails – Rails管理员以has_many嵌套形式隐藏belongs_to字段

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails管理员以has_many嵌套形式隐藏belongs_to字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个型号
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管理员我得到以下选项.

添加新的联系表格

添加新的实体表单

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

任何帮助都会有用.

解决方法

您可以使用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

If you set the :inverse_of option on your relations,RailsAdmin will
automatically populate the inverse relationship in the modal creation
window. (link next to :belongs_to and :has_many multiselect widgets)

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

让我知道它是怎么回事

猜你在找的Ruby相关文章