activeadmin: adding delete for a nested resource
Solved adding the following line:
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
The code looks like:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
end
end
f.buttons
end
How to handle multiple nested resources in ActiveAdmin?
Well, the solution is pretty simple...
https://github.com/josevalim/inherited_resources
ActiveAdmin.register Step do
controller do
nested_belongs_to :program, :knowledge
end
end
Nested resource with 2-deep association
I believe your ActiveAdmin registrations need to be reflected the same way as they are in your models.
For example, you have:
ActiveAdmin.register SupportSession do
belongs_to :service_user
but your SupportSession
model is defined as:
class SupportSession < ApplicationRecord
...
has_one :service_user, through: :support_allocation
...
Assuming your model is correct, the ActiveAdmin registration should also be a has_one
relationship.
Additionally, it looks like you have not fully defined your model relationships. Your ServiceUser
model does not have a corresponding belongs_to
relationship.
ActiveAdmin Nested Resource Index View
The issue is due to ActiveAdmin Sortable Tree's method of finding roots in the hierarchy. By default the sortable tree finds all roots for the model specified, regardless of nesting. This behavior may be customized by providing a proc
to the :roots_collection
option:
ActiveAdmin.register MenuItem do
belongs_to :menu
sortable tree: true,
# Only display the parent menu's roots
roots_collection: proc { parent.menu_items.roots }
end
Answer copied from my post on ActiveAdmin Sortable Tree #30.
active admin nested resources form generation
I had a similar issue in one of my activeadmin projects. The fieldset
element uses li
elements that you can style. So check the class the form is using, and than add something like this to the end of app/assets/stylesheets/active_admin.scss
. Here I assume the css class on the surrounding form is line_items
. The trick is the inline-block
property on the li
element, you may have to fiddle with the css properties though.
form.line_items fieldset.has_many_fields ol > li > label { display: none; }
form.line_items fieldset.has_many_fields:first-child ol > li > label { display: block; }
form.line_items fieldset.has_many_fields ol > li { display: inline-block; padding: 5px; width: 18%; float:left;}
form.line_items fieldset.has_many_fields ol > li.has_many_delete { margin-top:20px; margin-left: -36px;}
form.line_items fieldset.has_many_fields ol > li > label { width: auto; padding-right: 10px;}
form.line_items fieldset.has_many_fields ol > li > div > label.label { width: auto; padding-right: 10px;}
form.line_items fieldset.has_many_fields ol > li > a.has_many_remove { margin-top:20px; }
form.line_items fieldset.has_many_fields ol > li > p.inline-errors { margin: 0.3em 0 0 0; }
This would work but you will have to rewrite the form to:
f.inputs 'Line Item/s' do
f.has_many :payment_line_items, heading: false do |form|
form.input :description, label: false
form.input :quantity, label: false
form.input :unit_price, label: false
form.input :amount, label: false
end
end
If you want the user to be able to destroy line items you will have to set up your assocations and the permit_params for it like so:
In your model
accepts_nested_attributes_for :line_items, :allow_destroy => true
And in the activeadmin register block:
permit_params \
:line_items_attributes => [:id, :description, :quantity, :unit_price, :amount, :_destroy]
Good luck!
Nested has_many resource form with Active Admin doesn't do an update
I realized what I did wrong - I sort of over-thought it a bit. I didn't know that when you permit strong parameters, you have to also permit the :id
parameter on an associated record you're trying to update. I sort of assumed Rails magic would take care of that.
So it works if you change the permit_params
call to say this instead:
permit_params page_attributes_attributes: [:id, :key, :value, :_destroy => true]
In fact, that's what the Strong Parameters section on the Active Admin Github wiki says to do, I should have paid attention to why it was set up that way.
activeadmin has_many hide remove button
strange
from the documentation it looks like that not including :allow_destroy
is the solution for not having that destroy option
ActiveAdmin.register Post do
form do |f|
f.inputs 'Details' do
f.input :title
f.input :published_at, label: 'Publish Post At'
end
f.inputs 'Content', :body
f.inputs do
f.has_many :categories, heading: 'Themes',
allow_destroy: true,
new_record: false do |a|
a.input :title
end
end
f.inputs do
f.has_many :taggings, sortable: :position, sortable_start: 1 do |t|
t.input :tag
end
end
f.inputs do
f.has_many :comment,
new_record: 'Leave Comment',
allow_destroy: -> { |c| c.author?(current_admin_user) } do |b|
b.input :body
end
end
f.actions
end
end
The :allow_destroy option adds a checkbox to the end of the nested form allowing removal of the child object upon submission. Be sure to set allow_destroy: true on the association to use this option. It is possible to associate :allow_destroy with a string or a symbol, corresponding to the name of a child object’s method that will get called, or with a Proc object. The Proc object receives the child object as a parameter and should return either true or false.
also in some cases was necessary including the accepts_nested_attributes_for :images, allow_destroy: true
to include this option
I don't know how to solve this, maybe you should post an issue in their github page?
https://github.com/activeadmin/activeadmin/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20destroy
Related Topics
Generate a 'Link_To' to the Controller Action 'Edit', Dynamically
Static Site Generator Based Upon Directories & Files
Rails Server Does Not Start -> Could Not Find a JavaScript Runtime
How to Completely Wipe Rubygems Along with Rails etc
Bundle Install Not Running from My Post-Update Hook
Is There an Easy-To-Use Ftp Library for Ruby
Ruby on Rails HTML-Table Generator
Disable Devise's :Confirmable On-The-Fly to Batch-Generate Users
Different Background Color for Different Pages in Rails
Rails Render Partial in Helper
Are There "Rules" for Ruby Syntactic Sugar
Port in Use When Not Using a Port
Preventing Delayed_Job Background Jobs from Consuming Too Much CPU on a Single Server
What's the Easiest Way to Send a Message Through Outlook with Ruby