I have nested resources in active admin edit page, but I would like only to allow admin to edit content of existing resources, not to add new nested resources. My code looks like that:
form do |f|
f.inputs do
f.input :author
f.input :content
f.has_many :comments do |comment_form|
comment_form.input :content
comment_form.input :_destroy, as: :boolean, required: false, label: 'Remove'
end
end
f.actions
end
But it add's "Add new comment" button under inputs. How can I disable it, and leave only f.actions buttons for main form?
Starting from v0.6.1 you can pass new_record: false to hide the "Add new" button
f.has_many :comments, new_record: false do |comment_form|
...
end
The commit 4b58b8
Related
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :user_groups
end
Similarly, user_group.rb
class UserGroup < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now the active admin page for user groups is as shown below:
ActiveAdmin.register UserGroup, as: 'UserGroup' do
form do |f|
f.inputs do
f.has_many :usergroup_users, as: 'member user' do |user|
user.input :user_id
end
end
end
end
Now this will create the user group form with fields for adding users to the new group. The button for adding new user to the group will be Add new User Group User. I want to modify this button label to something else. How can it be implemented?
Part of my problem is solved when using:
f.has_many :usergroup_users, heading: 'Add User to Group', as: 'member user' do |user|
...
end
This will change the header part to the required text. But the button label is still be a problem.
f.has_many :usergroup_users, new_record: 'Add member user' do |user|
...
end
The new_record attribute can be used to customize the button label.
In this way, one can change the text on new and edit pages buttons.
form do |f|
f.inputs do
f.input :email
f.input :phone
end
f.actions do
if resource.persisted?
# edit
f.action :submit, as: :button, label: 'Update Model'
else
# new
f.action :submit, as: :button, label: 'Create Model'
end
f.action :cancel, as: :link, label: 'Cancel'
end
end
I'm still a beginner with rails but have you tried this?
user.input :user_id, :input_html => { :value => 'your text' }
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.
I'm using Active Admin and I have a one-to-many relationship between two models:
class WeeklyMenu < ActiveRecord::Base
has_many :menu_items
attr_accessible :menu_items
accepts_nested_attributes_for :menu_items
end
In the admin page for WeeklyMenu I'd like to display five menu_items. This is what my admin page looks like at the moment:
ActiveAdmin.register WeeklyMenu do
form do |f|
f.inputs "Details" do
f.input :title
f.input :week
end
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
f.buttons
end
end
That gives me a very nice interface for adding more menu_items, but I want the user to have five of them - no more, no less. How would I go about doing that?
Replace
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
with
f.inputs "Menu items" do
5.times do
f.object.menu_items.build
end
f.fields_for :menu_items do |m|
m.inputs do
m.input :title
end
end
end
May not be the best solution, but this should work...
The fields_for answer that #user946611 suggested didn't work for me, but the following code did:
f.inputs 'Menu Items' do
(5 - f.object.menu_items.count).times do
f.object.menu_items.build
end
f.has_many :menu_items, new_record: false do |m|
m.input :title
m.input(:_destroy, as: :boolean, required: false, label: 'Remove') if m.object.persisted?
end
end
That will always show 5 forms for items, whether they have that many created or not. The new_record: false disables the "Add New Menu Item" button.
If you want to edit the form again, the answer of #user946611 lacks of a condition to tell whether the menu_item exists, because when submit and edit the form there will generate another five menu_items.
so it should be:
f.inputs 'Menu Items' do
if !f.object.menu_items.present?
5.times do
f.object.menu_items.build
end
end
f.fields_for :menu_items do |m|
m.inputs do
m.input :title
end
end
end
Activeadmin defines callbacks that can be used for this sort of thing:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb#L157-L161
The after_build hook seems like an appropriate place to initialize a has_many relationship
ActiveAdmin.register WeeklyMenu do
after_build do |weekly_menu|
(5 - weekly_menu.menu_items.size).times do
weekly_menu.menu_items.build
end
end
form do |f|
f.inputs "Details" do
f.input :title
f.input :week
end
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
f.buttons
end
end
A report, has_many answers, and an answer belongs_to report. The nested form is displayed and works, but for the 5 answers I have
Add Answer
Answer __
Add Answer
Answer __
[next 3]
How can i group them in one single block with title "Add Answer", and 5 inputs ?
form do |f|
f.inputs "New Report" do
f.input :title
f.input :type
f.input :help_text
f.input :text_box_present
f.input :text_box_default
f.input :template_id
end
f.inputs "ADD Answers", for: :answers do |a|
a.input :text, label: "Answer", required: false
end
f.actions
end
Wow, this is some elegant stuff that active admin and formtastic provides for nested attributes
f.inputs "Answers" do
f.has_many :answers do |a|
a.input :text, label: "Answer", required: false
end
end
It will auto add a delete/add button with javascript, for adding multiple answers
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