Rails admin hide belongs_to field in has_many nested form
Asked Answered
M

2

6

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 am getting below options.

Add new Contact Form

enter image description here


Add new Entity Form

enter image description here

I need to hide Entity field in contact form , while adding new entity.

Any help will be useful.

Meso answered 9/10, 2013 at 12:26 Comment(5)
Have you resolved this issue yet? I don't understand your question. so a user navigates to 'new contact' form and then they click "add a new entity" and it appears above the 'new contact' form. At that moment you want to hide the "add new entity" button below?Rosenberger
No, Basically I am trying to add New Entity. Since Entity has many contacts , Rails admin generating nested form to add contacts while adding entity itself. But when i click on add new Contact in Entity adding form , it asking me to Select entity , it is meaning less , since I am adding contact to the entity which is not yet created. Got it? Else I can explain more.Meso
So when you create a new entity form with a nested new_contact form it is showing a select box to choose the entity? If what I understood is correct, you can probably hide it in the views. Please share the form's code.Rosenberger
@Rosenberger : I understand I can hide it in Forms. But again problem in I can add contacts directly through contacts adding form also. I think Only option is to write Custom View for adding contacts.Meso
yeah from what I understand I think you will need to write a custom view to fine tune your requirements.Rosenberger
T
12

You can automatically hide the 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 on your relations, RailsAdmin will automatically populate the inverse relationship in the modal creation window. (link next to :belongs_to and :has_many multi-select widgets)

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

Let me know how it went

Theona answered 26/11, 2013 at 18:12 Comment(0)
R
6

For the sake of completness and because i had this problem too and solved it, if you want to you can configure a model when it is used inside a nested form just like you do with edit, update, create and nested

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 info

Ress answered 14/2, 2014 at 23:11 Comment(1)
This is the correct answer if you don't want to show all the fields in the nested form.Constitution

© 2022 - 2024 — McMap. All rights reserved.