加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 21:30:10 所属栏目:百科 来源:网络整理
导读:我有两个型号 class Entity ActiveRecord::Base # Associations has_many :contacts accepts_nested_attributes_for :contacts,:allow_destroy = trueendclass Contact ActiveRecord::Base # Associations belongs_to :entityend 现在在rails管理员我得到以
我有两个型号
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

让我知道它是怎么回事

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读