I have parent has many children relation. I want to easily manage children of specific parent.
I am trying like this:
form do |f|
f.inputs "Parent" do
f.input :name
end
f.inputs 'Children' do
f.has_many :children, new_record: true do |c|
c.input :name
end
end
f.actions
end
But I am getting:
undefined method `new_record?' for nil:NilClass
I have Rails 5. Is there a better way to make this work? What is the best way to allow user to manage child objects?
f.has_many :children do |c|
c.inputs "Children" do
c.input :name
#repeat as necessary for all fields
end
end
Make sure to have this in your parent model:
accepts_nested_attributes_for :children
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
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
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