Is there anyway to generate Active Admin's views? I know how to override them but I'd like to keep their basic layout but just add some nested forms.
According to Active Admin Documentation, there is no way to generate all views:
You can create forms with nested models using the has_many method:
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" do
f.input :body
end
f.inputs do
f.has_many :categories, :allow_destroy => true, :heading => 'Themes', :new_record => false do |cf|
cf.input :title
end
end
f.actions
end
end
The :allow_destroy option will add 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.
The :heading option will add a custom heading to has_many form. You can hide a heading by setting :heading => false.
The :new_record option will show or hide new record link at the bottom of has_many form. It is set as true by default.
Related
Example models:
class Person
has_many :auto_mappings, dependent: :destroy
class Vehicle
has_many :auto_mappings
class AutoMapping
belongs_to :person
belongs_to :vehicle
validates :person,
:vehicle,
:usage,
presence: true
In my ActiveAdmin form for Create/Edit on Person, I include an input f.has_many for the nested AutoMapping resource to add a single AutoMapping at a time, which works great. What I would like to accomplish is to somehow add a button that would add an AutoMapping for each Vehicle to the Person.
Is there an approach to accomplish this or is there another direction I should go?
Example AA form for Person:
form do |f|
f.inputs 'Details' do
f.input :name
f.input :age
f.has_many :auto_mappings, class: 'auto_mapping' do |item|
item.input :vehicle, include_blank: false, input_html: {class: "vehicle", disabled: item.object.persisted?}
item.input :usage,
as: :select,
collection: %w[drive ride both]
include_blank: false
end
end
# Maybe a custom member action to "Create Person with all Auto Mappings"?
# I would prefer to auto populate the form page with `AutoMappings` rather than create them all on Submit if possible.
f.actions
end
Thanks!
You would have to extend has_many.es6 to make an Ajax call and add the items.
I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the
Ability to add and delete relations for existing records as well.
Ideally there should be an autocomplete box that allows searching and selection of an existing meaning for this particular model.
My models are
Word
Meaning
WordMeaning
I only want the capability to attach meanings that are already available to a word?
class Word < ActiveRecord::Base
belongs_to :language
has_many :word_meanings
has_many :meanings ,through: :word_meanings
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :language
f.input :word
f.input :wordInScript
f.input :pronunciation, :required => false, :as => :file
end
f.inputs do
f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
a.input :meaning
a.input :language
end
end
f.actions
end
You can determine the collection of the select:
a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }
ActiveAdmin uses Formtastic:
https://github.com/justinfrench/formtastic#usage
In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.
Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end
For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.
I am making a blog using rails and activeadmin. I associate tags to each post. I want to know how to configure activeadmin to add tags on posts.
I want to be able to create tags from a new Post form. How can i do that?
Thank you
if Posts has_many Tags then something like this could work.
form do |f|
f.inputs do
f.input :title
f.input :content
f.has_many :tags do |fu|
fu.input :name
fu.input :_destroy, :as=>:boolean, :required => false, :label => 'Delete Tag' unless fu.object.new_record?
end
end
end
ActiveAdmin expects that the Post model accepts_nested_attributes_for :tags
like..
class Post
#....
accepts_nested_attributes_for :tags
end
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