-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Feature Request: Nested Forms #192
Comments
hey, @ismaGNU - thanks! Glad to hear you like it! You're right - at the moment we don't support nested forms. There are a couple ways you could implement it yourself, though. If we're sticking with the
This would create:
You can inherit from diff --git a/spec/example_app/app/fields/nested_line_item_field.rb b/spec/example_app/app/fields/nested_line_item_field.rb
index e69de29..73e2d7d 100644
--- a/spec/example_app/app/fields/nested_line_item_field.rb
+++ b/spec/example_app/app/fields/nested_line_item_field.rb
@@ -0,0 +1,4 @@
+require "administrate/fields/base"
+
+class NestedLineItemField < Administrate::Field::HasMany
+end
diff --git a/spec/example_app/app/views/fields/nested_line_item_field/_form.html.erb b/spec/example_app/app/views/fields/nested_line_item_field/_form.html.erb
index e69de29..78ae901 100644
--- a/spec/example_app/app/views/fields/nested_line_item_field/_form.html.erb
+++ b/spec/example_app/app/views/fields/nested_line_item_field/_form.html.erb
@@ -0,0 +1,7 @@
+<%= f.label field.attribute_key %>
+
+<%= f.fields_for field.attribute_key do |nested_form| %>
+ <fieldset>
+ <%= nested_form.text_field :nested_attribute %>
+ </fieldset>
+<% end %>
diff --git a/spec/example_app/app/views/fields/nested_line_item_field/_index.html.erb b/spec/example_app/app/views/fields/nested_line_item_field/_index.html.erb
index e69de29..758e0c5 100644
--- a/spec/example_app/app/views/fields/nested_line_item_field/_index.html.erb
+++ b/spec/example_app/app/views/fields/nested_line_item_field/_index.html.erb
@@ -0,0 +1 @@
+<%= pluralize(field.data.count, field.attribute.to_s.humanize.downcase) %>
diff --git a/spec/example_app/app/views/fields/nested_line_item_field/_show.html.erb b/spec/example_app/app/views/fields/nested_line_item_field/_show.html.erb
index e69de29..1166a16 100644
--- a/spec/example_app/app/views/fields/nested_line_item_field/_show.html.erb
+++ b/spec/example_app/app/views/fields/nested_line_item_field/_show.html.erb
@@ -0,0 +1,20 @@
+<% if field.resources.any? %>
+ <%= render(
+ "collection",
+ collection_presenter: field.associated_collection,
+ resources: field.resources
+ ) %>
+
+ <% if field.more_than_limit? %>
+ <span>
+ <%= t(
+ 'administrate.fields.has_many.more',
+ count: field.limit,
+ total_count: field.data.count,
+ ) %>
+ </span>
+ <% end %>
+
+<% else %>
+ <%= t("administrate.fields.has_many.none") %>
+<% end %> And here are some places in the codebase you might want to use for reference: Right now we're focusing on stability, so it might be a while before we get to this. If you can get it working well for your project, let us know how you did it - that's probably the best way to push this forward. Let me know if you have any questions, or if I can help out at all! |
Thanks @Graysonwright, that was my first approach, building LineItemField and inherits from HasMany one. The problem is that I want to have a button to add as line items as I want. So I am stuck at this part. Anyway, I really appreciate your help and I´ll let you know my progress 💃 |
@ismaGNU Do you have some sample code on Github for your implementation of nested form fields? |
@5minpause no man, I am not be able to make it works like I expected. Do you have any ideas? I just make what I told before, but It doesn´t work. |
Not sure if this is exactly what the OP had in mind, but I've been able to incorporate Cocoon into Administrate, but with some hacks. I'll swing back around and work on a way to create a PR (or several) out of this work, because I think this is pretty important to have. |
@mcmire - any updates? |
@Graysonwright Not yet unfortunately, I haven't had much time lately. |
@airjoshb I've added nested params by overriding the
module Admin
class AuthorsController < Admin::ApplicationController
def resource_params
params.require(:author).permit(
permitted_attributes,
social_links_attributes: [
:id,
:value,
:title
]
)
end
end
end Would love to see nested forms as part of Administrate. |
@nburkley Your answer got me going and I devised a slightly less "invasive" approach. In the Dashboard for the model in question, you can define permitted_attributes. I now have cocoon working flawlessly! Here is a peek at my formatting
|
@airjoshb that's a great approach! I'll take a crack at officially supporting & documenting that. |
ok, I managed to recreate this in one of my own apps. I think there are some things we can do to make this process more automated, with less user input. I'm hopeful that we can build out some kind of |
@Graysonwright Here are my files:
and admin/application/_answer_fields.html.erb
|
@airjoshb thanks! |
@Graysonwright I haven't added nested forms yet, just added an MVP with allowed editing some child values that we build |
Closes #192 Feature: When I create or edit a model that has_many nested models, I want to view and edit the attributes of the nested models so I can set up all the relationships with a single form. Implementation: Introduce a new field type, `NestedHasMany`. I considered building the feature into the existing `HasMany` field, but this would get in the way of `HasMany` relationships that aren't directly nested, such as in many-to-many relationships. The `NestedHasMany` field builds off of the [Cocoon] gem. It renders fields from the nested model, based on the fields defined in the nested model's dashboard class. Cocoon provides helpers and javascript to easily add and remove nested form fields from the page. [Cocoon]: https://github.com/nathanvda/cocoon
In #476, we decided on a plugin structure for new field types. Nested has_many forms are a good candidate for a plugin, so I extracted this code out to https://github.com/graysonwright/administrate-field-nested_has_many. It's rough but functional. PRs are welcome on that repository. If we get it polished enough and it turns out to be useful enough, we can absorb it into this core repository. |
nice, thanks @Graysonwright! |
very cool. Thanks! @Graysonwright |
nice! @Graysonwright, can this be used with paperclip as well? I'm trying to do nested pictures for products. |
Also, I think this line #7 require "administrate/field/image" needs to be removed. It looks like image has been moved to a plugin. thanks again! |
Hi guys,
Thanks in advance for this great project, really good job. I was looking for something like that such a long time.
I was digging around and I have missed and option to build nested forms. I don´t know if there is an easy way to do it. IMHO its needed for the example app when you add line items to an order. Its painful to first create an order, save it then create line items and associate it. This happens on all kind of database models and It would be amazing to have this feature.
I think you can achieve this behaviour by adding custom fields types, overriding form view. Although you must allow nested attributes on your models.
Let me know what do you think about it and set a guidelines to approach this feature.
Thanks in advance and keep pushing!
The text was updated successfully, but these errors were encountered: