Nested in Forms
You can use "Show on edit screens" to make the field available in the edit view. However, this will render it using the show view component.
To enable nested creation for the field, allowing it to be created and / or edited alongside its parent record within the same form, use the nested option which is a hash with configurable option.
The avo-nested gem
Nested association forms are provided by the avo-nested gem. Add it to your Gemfile using the same private gem source as your other Avo paid gems:
gem "avo-nested", source: "https://packager.dev/avo-hq/"Run bundle install. If you have not set up packager.dev access yet, see Gem server authentication.
Keep in mind that this will display the field’s resource as it appears in the edit view.
-> nested
Enables this field as a nested form in the specified views.
Default value
{}
Possible values
A hash with the following options:
on:Views in which to enable nesting. Accepted values:limit:(Only forhas_manyandhas_and_belongs_to_manyfields) Hides the "Add" button when the specified limit is reached.
TIP
Setting nested: true is a shortcut for nested: { on: :forms }.
Deleting existing records
Sinceavo-nested >= v4.3.3A record added in the form but not saved yet can be removed at any time. An existing record gets a "Delete" button only when the parent model opts in with allow_destroy: true:
# app/models/book.rb
accepts_nested_attributes_for :authors, allow_destroy: trueClicking it asks for confirmation, then marks the record for deletion. It stays in the form, dimmed, with an "Undo" link, and nothing happens until the parent form is saved. Saving destroys the record. See Authorization for the policy check that applies.
Example
# app/avo/resources/book.rb
class Avo::Resources::Book < Avo::BaseResource
def fields
# Shortcut for full nesting
field :authors, as: :, nested: true
# Explicit nesting on new only
field :authors, as: :, nested: { on: :new }
# Explicit nesting on edit only
field :authors, as: :, nested: { on: :edit }
# Explicit nesting on both new and edit
field :authors, as: :, nested: { on: :forms }
# Limit nested creation (for has_many or has_and_belongs_to_many only)
field :authors,
as: :,
nested: { on: [:new, :edit], limit: 2 }
end
endAuthorization
With authorization enabled, a nested form writes a child only when the child's own pages would let the user write it.
- Existing records are saved only when they are inside the child resource's policy scope and the child policy allows
update?.update?is asked twice: once with the record as loaded, and again with the submitted values. A row the user may not update is skipped, and the rest of the form still saves. - Records marked for deletion are destroyed only when they are inside the policy scope and the child policy allows
destroy?. Ifdestroy?refuses, the save is refused. - New records need the child policy's
create?and the parent policy'screate_authors?. If either refuses, the save is refused. - When
create_authors?refuses, the form hides its "Add" button, the same way the association panel hides "Create new".
INFO
With explicit_authorization on, a parent policy that doesn't define create_authors? refuses new rows. Define it to let users add records from the form:
# app/policies/book_policy.rb
class BookPolicy < ApplicationPolicy
def create_authors?
user.admin?
end
end