Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails admin hide belongs_to 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 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.

like image 416
Senthil Avatar asked Oct 09 '13 12:10

Senthil


2 Answers

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

like image 119
iuval Avatar answered Sep 28 '22 02:09

iuval


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

like image 31
Guillermo Siliceo Trueba Avatar answered Sep 28 '22 03:09

Guillermo Siliceo Trueba