Rails admin hide belongs to a field in has_many nested form

I have two models

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 

Now in rails admin I get below options.

Add new contact form

enter image description here


Add a new entity form

enter image description here

I need to hide the Entity field in the contact form by adding a new object.

Any help would be helpful.

+6
source share
2 answers

You can automatically hide fields using inverse_of like this

 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 in your relationship, RailsAdmin will automatically populate the feedback in the create modal window. (link next to: assign_to and: has_many multiple widget selection)

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

Let me know how it went

+9
source

For completeness, and because I also had this problem, and I solved it if you want, you can customize the model when it is used in a nested form, just as you do with editing, updating, creating, and nesting.

 class Contact < ActiveRecord::Base # Associations belongs_to :entity rails_admin do nested do configure :entity do hide end end end end 

Visit the official wiki for more information.

+5
source

Source: https://habr.com/ru/post/955573/


All Articles