Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Admin - removing a related object

I'm using Rails Admin on one of my sites. It's great so far, but I can't figure out how to remove a related object from an edit page.

Example: I have two models Property and PropertyImage.

class Property
  has_many :property_images, :dependent => :destroy
end

class PropertyImage
  belongs_to :property
end

I can go to the edit screen for an instance of either model, and I can delete PropertyImages from their list view. But when I edit a Property, I want to be able to delete a PropertyImage that's associated with it. Is there a way to turn on this functionality in rails_admin?

Here's what I can see.

rails_admin interface

Note: the "Delete Image" button isn't what I'm looking for - it's just because there's an upload association to the Image field. It only edits the PropertyImage.

like image 710
Steve Davis Avatar asked Jan 16 '23 23:01

Steve Davis


1 Answers

I had this same question, and found an answer that works for me after reading your question.

In order to properly setup up the editing of PropertyImage from the Property form, you probably want to specify that it can work with the nested form:

# property.rb
class Property
  has_many :property_images, :dependent => :destroy
  accepts_nested_attributes_for :property_images, :allow_destroy => true
end

Including the :allow_destroy option should make the delete option show up for the nested item.

Screenshot of nested form with delete button

like image 124
brookr Avatar answered Jan 26 '23 03:01

brookr