Skip to content
This repository

Nested forms #59

Closed
kupinus opened this Issue May 18, 2011 · 27 comments
kupinus
kupinus commented May 18, 2011

Hello! I am looking at ActiveAdmin as a candidate to be used in my next project, I like it a lot.

However in all the examples, the edit form only edits the attributes of the given model. Is it possible to somehow edit associated models like Contacts on Person page (contact belongs_to person)?

I have seen some support for this in Formtastic, but I have no idea if it is possible in ActiveAdmin - how to make "add contact" button on Person page etc...

Thanks in advance for the reply!
Jakub Cerny

Ryan T
ryanto commented May 18, 2011

if your models are using accepts_nested_attributes_for then editing their relations is very simple in activeadmin.

as far as adding a new contact onto an existing person, just have a link on your edit person page to new_admin_contact_path (or whatever the correct route is).

Erik Östling

First I'd like to say thanks for an awesome job with AA!

I'm also interested in this issue and I got it working by using accepts_nested_attributes_for and fields_for. However I'm stuck on how to add a button in the form to add another associated model. In my example I'm using a Blog model which has many Posts.

Is there some smart way to do it with AJAX kind of like in this Railscast? http://railscasts.com/episodes/197-nested-model-form-part-2

Erik Östling

By the way, here's my blogs.rb

ActiveAdmin.register Blog do
  form do |f|
    f.inputs do
      f.input :title
    end

    f.inputs "Posts" do
      f.semantic_fields_for :posts do |j|
        j.inputs :title, :text
      end
    end

    f.buttons
  end
end
Jarin Udom

Yeah this is a big one for me too, I need to be able to add multiple child elements (for example, adding images to a gallery).

Scott

@erikostling @jarinudom

It appears that there is a method called has_many in the form builders that does exactly what you are looking for. Simply change line 8 of the above code snippet from f.semantic_fields_for ... to f.has_many ... and you should be pleasantly surprised.

ActiveAdmin.register Blog do
  form do |f|
    f.inputs do
      f.input :title
    end

    f.inputs "Posts" do
      f.has_many :posts do |j|
        j.inputs :title, :text
      end
    end

    f.buttons
  end
end
Jarin Udom

Well shit. I just got pleasantly surprised! Thanks man :)

Scott

Indeed. has_many support in the form builder is easily one of the most awesome features in active_admin. (imo)

Jarin Udom

Oh super, I found kind of a hacky way to get custom content into the form builder:

f.has_many :images do |j|
  j.form_buffers.last << "<img src='#{j.object.thumb_url rescue nil}' style='float:left;margin:1em;' />".html_safe
  j.input :position
  j.input :file, :as => :file
end

The 'rescue nil' is because it fails on a new record. It would definitely be nice to have a smoother way to do it, although I'm not sure what the best way to go about it would be.

Scott

@jarinudom Nice find. =) I did find that wrapping the image tag in a list item (li) tag and giving it a label helped it to fit better with the default form layout.

Erik Östling

@scottswezey WOW! That was exactly what I was looking for. :) Thanks!

Luiz Fernando Signorelli

has_many is great indeed, but on the edit page it does not show a button to delete the associated model. Is there a way to do this?

Scott

@luizsignorelli

The delete button will only show up on new sub records (before they are saved). If you want to delete them after that, you should add an attribute named :_destroy (as a checkbox or similar). When set to true, the sub-record will be deleted. You will also need the :allow_destroy => true option passed to the model's allow_nested_attributes_for

PS: I tend to confuse destroy vs delete. If destroy is incorrect or does not work, try delete. =)

Luiz Fernando Signorelli

Ok, here is what I did to show a delete checkbox only on edit page:

form do |f|
    f.inputs
    f.has_many :contacts do |c|
      if !c.object.id.nil?
        c.input :_destroy, :as => :boolean, :label => "delete"
      end
      c.inputs :name, :phone, :email
    end
end

Strangely, if I put the checkbox in the bottom, I get an error:

form do |f|
    f.inputs
    f.has_many :contacts do |c|
        c.inputs :name, :phone, :email
        if !c.object.id.nil?
           c.input :_destroy, :as => :boolean, :label => "delete"
        end
    end
end

Any hints?

Ian Murray

@luizsignorelli the same thing happens to me. It's pretty strange.

Michael Hellein

I've been attempting to use a nested form for a has_one relationship, by calling semantic_fields_for, and I'm getting no output from my nested form fields. I'm going to try digging into the form_builder source, but I was hoping someone might have some insight. (Rails 3.1 rc6)

Dom

It's strange behavior to allow delete on a new record and not in the edit action. I think in the edit form, the delete button or checkbox should automatically be added if the model has the :allow_destroy => true option on the allow_nested_attributes_for method. It would be even nicer if it worked exactly like it does on the new form with ajax removing the entry. Perhaps this can be added as feature?

Láďa Durchánek

+1 to @MeetDom, this would be nice feature.

Philippe Creux
Collaborator

I'm closing this issue as it's resolved.

@MeetDom: feel free to create an issue for the feature you were talking about. :)

Philippe Creux pcreux closed this September 27, 2011
Pourrier François
fro commented January 31, 2012

@MeetDom: did you create an issue for this (can't find it)?
@pcreux: is the feature implemented in the last version of AA?

Juergen Fesslmeier

@michaek +1 same problem, resolved?

Michael Hellein

This one is such a vague issue that I don't think it should be reopened.

Regarding nested has_one: the most recent activity on #459 (comment) suggests that you just need to build a record on the association before trying to use it in the form. I haven't tested it, but there seems to be consensus on that.

BrightBytes

To avoid the issue reported by luizsignorelli with the delete button at the bottom, do this:

f.has_many :guesses do |ff|
  # whatever
  if ff.object.id
    ff.input :_destroy, :as => :boolean, :label => "delete"
  end
  ff.form_buffers.last # to avoid bug with nil possibly being returned from the above
end
Prashant Dixit

what about one to one association

seangraph

do not forget in order to be able to use allow_nested_attributes_for you need add to your gem file 'nested_form'.

Marcus Geißler

for has_one this works for me:

  form do |f|
    f.inputs :for => [:announcement, (f.object.announcement || f.object.build_announcement)] do |a|
      a.input :file
      a.input :file_cache, :as => :hidden
    end
  end
Alexander

@marcusg Tried this but it creates a new record in database for nested attribute even if I have one that is associated with model.

Marcus Geißler

@Zamyatin-AA Can you try to debug this? What is f.object.your_model_name in case of update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.