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
Comments
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). |
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 |
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 |
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). |
It appears that there is a method called 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 |
Well shit. I just got pleasantly surprised! Thanks man :) |
Indeed. has_many support in the form builder is easily one of the most awesome features in active_admin. (imo) |
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. |
@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. |
@scottswezey WOW! That was exactly what I was looking for. :) Thanks! |
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? |
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 PS: I tend to confuse |
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? |
@luizsignorelli the same thing happens to me. It's pretty strange. |
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) |
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 |
+1 to @MeetDom, this would be nice feature. |
I'm closing this issue as it's resolved. @MeetDom: feel free to create an issue for the feature you were talking about. :) |
@MeetDom: did you create an issue for this (can't find it)? |
@michaek +1 same problem, resolved? |
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. |
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 |
what about one to one association |
do not forget in order to be able to use allow_nested_attributes_for you need add to your gem file 'nested_form'. |
for
|
@marcusg Tried this but it creates a new record in database for nested attribute even if I have one that is associated with model. |
@Zamyatin-AA Can you try to debug this? What is |
I encountered @Zamyatin-AA 's issue: when doing an |
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, |
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
The text was updated successfully, but these errors were encountered: