I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.
Sep 2017 Update:
Rails 5.1.4, ActiveAdmin 1.0.0
Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.
Final look:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
ActiveAdmin Reference
This worked for me:
i.input :_destroy, as: :boolean
and in the Model remember to add :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
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
If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params
permit_params :id,:name, :cod, :_destroy
I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.inputs :datacenters do
datacenter_form.input :name
end
datacenter_form.buttons do
link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
end
end
end
f.buttons
end
and the controller method should be defined in datacenters.rb
controller do
def destroy
#datacenter = Datacenter.find(params[:id])
#datacenter.destroy
redirect_to edit_admin_retailer_path(#datacenter.infrastructure)
end
end
This should work:
datacenter_form.label :_delete
datacenter_form.check_box :_delete
This adds a checkbox for each nested object which will delete the object if checked.
Don't forget to add the following to your parent model
has_many :child_name, :dependent => :destroy
Related
ActiveAdmin automatically pulls up the persisted information for an object when the update form is displayed. It doesn't show the images though - doesn't even show the name in my form - how do I fix this to show the actual image and it's name?
ActiveAdmin.register Art do
permit_params :art_pic_attachments_attributes: [:id, :picture, :_destroy]
form(html: { multipart: true }) do |f|
f.inputs do
f.has_many :art_pic_attachments, allow_destroy: true, heading: 'Images' do |ff|
ff.file_field :picture, required: true, heading: "Pictures"
end
end
f.actions
end
Maybe this wiki article will help. The key is to use an image tag as the input hint.
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => image_tag(f.object.cover_page.url)
f.input :cover_page_cache, :as => :hidden
end
My ActiveAdmin index page for ad_images works fine but new, edit, and view look blank like this...
admin/ad_image.rb looks like this:
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form(:html => { :multipart => true }) do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
The AdImage model works normally otherwise. Any clue what's going on?
UPDATE
Turns out that "ad_image" conflicts with a formtastic style of the same name... I think? When I remove the "ad_image" class from the form (which is added automatically) everything works fine.
I can give an alias to my ActiveAdmin class like so
ActiveAdmin.register AdImage, as: "ImagesForAds"
... and the form will appear. But now I can't save anything...
Edit admin/ad_image.rb :---
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
I have a nested form in ActiveAdmin for these models (a :class_section has_many :class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
and
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Everything seems to be in the right place when I look at the form. However, when I update a class_date, it doesn't save.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: false do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
index do
column :class_course
column :location
default_actions
end
filter :class_course
filter :location
show do |cs|
attributes_table do
row :class_course do
cs.class_course.id_num + " - " + cs.class_course.name
end
row :location
row :min_students, label: "Minunum Students"
row :max_students, label: "Maxiumum Students"
row :info, label: "Additional Information"
end
panel "Dates" do
attributes_table_for class_section.class_dates do
rows :start_time, :end_time
end
end
active_admin_comments
end
end
Here is the ActiveAdmin file for ClassDates:
ActiveAdmin.register ClassDate, as: "Dates" do
permit_params :start_time, :end_time, :class_section_id
belongs_to :class_section
end
Can you see a reason why it's not saving properly?
UPDATE: I added the following code to the AA and it seems to work now:
controller do
def permitted_params
params.permit!
end
end
Let me know if there is a better solution. Thanks.
UPDATE 2: There is one lingering problem however. I am unable to delete ClassDates using this form.
You need to permit nested parameters, but you should never use params.permit!. It's extremely unsafe. Try this:
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id,
class_dates_attributes: [ :id, :start_time, :end_time, :_destroy ]
form do |f|
# ...
f.inputs "Dates" do
f.has_many :class_dates, heading: false, allow_destroy: true do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
# ...
end
The configuration (and permitted_params) of your ClassDate admin panel has nothing to do with the permitted parameters within the ClassSection admin panel. Treat them as separate controllers within the app.
Adding the allow_destroy: true option to the has_many call will add a checkbox to the form to allow you to delete a class time upon form submission.
I looked at this question and was able to get set up a has_many form properly.
Here's where I'm at:
f.has_many :related_contents do |rc|
rc.inputs "first configuration" do
#...
end
end
f.has_many :related_contents do |rc|
rc.inputs "second configuration do
#...
end
end
so I want to have different configurations of fields. What I can't figure out is how to set the <h3> that activeadmin generates and sets as the title for the nested fields. I thought something like
f.has_many :related_contents, :title => "first set" do |rc|
would work, but It does not. Does anybody know what I need to do to get this working right?
I assume that changing the <h3> will also change the button that gets generated to add. Is there another option I need to set for that as well?
Add a :heading option to the f.has_many method
f.has_many :related_contents, :heading => "first set" do |rc|
#...
end
To remove the heading altogether, set it to false or nil
$('h3:contains("Related Contents")').hide().first().show();
Not perfect, but it definitely looks better.
you need to follow the next order, i mean, you need to set on first line the "inputs" and then the "has_many" block, I hope this is what you are looking, regards!
form do |f|
f.inputs "details #1" do
f.input :title, :label => "Title"
f.input :description, :label => "Description"
f.input :image, :as => :file, :required => false, :label=>'Image'
end
f.inputs "details #2" do
f.has_many :subdetails do |a|
a.input :description, :label => "Description"
a.input :image, :as => :file, :required => false, :label=>'Image'
end
end
Ok, after have some problems with the title of has_many I could figure out how is the correct way for do it and i think is more cleaner and fancy...
form do |f|
f.inputs "Post" do
f.input :title
f.input :description
end
f.inputs "Comments" do
f.has_many :comments do |a|
a.input :title
a.input :description
end
end
f.buttons
end
I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. --
(page has_one meta_data, accepts_nested_attributes_for meta_data)
Example 1)
in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...
form do |f|
f.inputs "Page Information" do
f.input :name
f.input :uri
f.input :view
f.input :body, :as => :text
f.input :active
end
f.inputs "Meta Data" do
f.semantic_fields_for :meta_data do |meta_form|
meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
end
end
end
I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this
2) How I am working around it, but seems hacky
<%= semantic_form_for [:admin, #page] do |f| %>
<% #page.build_meta_data %>
<%= f.inputs :name => "Page Information" do %>
<%= f.input :name %>
<%= f.input :uri %>
<%= f.input :view %>
<%= f.input :body, :as => :text %>
<%= f.input :active %>
<% end %>
<%= f.semantic_fields_for :meta_data do |meta_form| %>
<%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
<% end %>
<%= f.buttons %>
<% end %>
Thanks in advance for any help or clarification.
(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)
I found a better solution for you. You can use :for option in inputs helper.
f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
meta_form.input :title
meta_form.input :description
meta_form.input :keywords
end
I think this might work too, but I didn't check
f.inputs :title, :desctiption, :keywords,
name: "Meta Data",
for: [:meta_data, f.object.meta_data || MetaData.new]
In rails 4, this is something that works, with a nice design
e.g.,
A customer has one account
model/customer.rb
accepts_nested_attributes_for :account
admin/customer.rb
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
i was having the same problem, i worked in your hack and got it working.
i then moved <% #page.build_meta_data %> to a custom new method like this
controller do
def new
#tenant = Tenant.new
#tenant.build_tenant_configurable
end
end
hope this helps