Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested forms #59

Closed
cernyjakub opened this issue May 18, 2011 · 29 comments
Closed

Nested forms #59

cernyjakub opened this issue May 18, 2011 · 29 comments

Comments

@cernyjakub
Copy link

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

@ryanto
Copy link

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).

@erikostling
Copy link

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

@erikostling
Copy link

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

@jarinudom
Copy link

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).

@scottswezey
Copy link

@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

@jarinudom
Copy link

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

@scottswezey
Copy link

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

@jarinudom
Copy link

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.

@scottswezey
Copy link

@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.

@erikostling
Copy link

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

@luizsignorelli
Copy link

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?

@scottswezey
Copy link

@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. =)

@luizsignorelli
Copy link

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?

@ianmurrays
Copy link

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

@michaek
Copy link

michaek commented Aug 26, 2011

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)

@jocubeit
Copy link

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?

@durchanek
Copy link

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

@pcreux
Copy link
Contributor

pcreux commented Sep 27, 2011

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

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

@pcreux pcreux closed this as completed Sep 27, 2011
@fro
Copy link

fro commented Jan 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?

@chinshr
Copy link

chinshr commented Feb 8, 2012

@michaek +1 same problem, resolved?

@michaek
Copy link

michaek commented Feb 8, 2012

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
Copy link

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

@prashant85
Copy link

what about one to one association

@seangraph
Copy link

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

@marcusg
Copy link
Contributor

marcusg commented Jul 24, 2013

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

@stekatk
Copy link

stekatk commented Mar 27, 2014

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

@marcusg
Copy link
Contributor

marcusg commented Mar 27, 2014

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

@thesteady
Copy link

I encountered @Zamyatin-AA 's issue: when doing an f.has_many in my edit form, existing relationships in my has_many, :through were getting duplicated. I needed to add a hidden field with the id of the join table and update the accepted attributes to get this problem to go away. Thought I'd report in case anyone else stumbles on this issue!

@mplewis
Copy link

mplewis commented Jun 30, 2017

Sorry for gravedigging, but I came upon this thread trying to learn how to build a nested form and it's extremely helpful.

In the latest build of ActiveAdmin, f.buttons has been deprecated and removed. You will need to use f.actions instead. Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests