ActiveAdmin Form Multiple Nested HMT relationships with has_many - ruby-on-rails

i'm new to ActiveAdmin and Rails and i struggle on something to build up my ActiveAdmin interface.
Consider the following models :
class PageType < ActiveRecord::Base
has_many :fields, class_name: 'PageField'
accepts_nested_attributes_for :fields, allow_destroy: true
end
class PageField < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields
has_many :pages, through: :page_has_fields
accepts_nested_attributes_for :page_has_fields, allow_destroy: true
end
class PageHasField < ActiveRecord::Base
belongs_to :page
belongs_to :page_field
end
class Page < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields, dependent: :delete_all
has_many :page_fields, through: :page_has_fields
accepts_nested_attributes_for :page_fields, allow_destroy: true
end
In Active Admin I want to create some page templates to handle "static" pages. And in each of the pages, I want to update the content of each fields related to the templates page.
Thus far, what I did worked with this code :
ActiveAdmin.register Page do
permit_params :name, :page_type_id, :page_id,
:page_fields_attributes => [:id, :name, :field_type, :page_id,
:page_has_fields_attributes => [:id, :content, :page_id]
]
form do |f|
f.inputs
f.has_many :page_fields, heading: false, new_record: false do |g|
g.inputs :name, :required
g.has_many :page_has_fields, new_record: false do |h|
h.input :content if h.object.page_id == f.object.id
end
end
f.actions
end
end
But the second has_many seems really wrong to me, and i'm sure there are a better solution to this problem.
If i don't go with the "if", inputs are created for the right fields, but for every single page.
Is there a way to specify an ID or a parameter in has_many ? Or a better tag to handle situation like this ?
Thanks

Try changing your setup to something more like this
ActiveAdmin.register Page do
...
form do |f|
f.inputs do
f.input :some_column
f.input :some_other_column
f.input :page_fields, as: :check_boxes, checked: PageField.all.map(&:name)
f.input :page_has_fields, as: :check_boxes, checked: PageField.all.map(&:content)
end
f.actions
end
end

Related

ActiveAdmin: Add association on object creation

When creating a new object and connecting it with existing (has_many :through) resources I need to:
Save the new object first
Edit this newly created object again to add connections to the nested resources.
Cumbersome! It seems ActiveAdmin tries to create the association first, then the main object. Is it somehow possible to do this object creation + associating nested resources in one go?
Is case a more concrete example is needed, here is an example of my data model and ActiveAdmin setup:
Person < ActiveRecord::Base
has_many :organizations, through: :person_organizations
end
Organization < ActiveRecord::Base
has_many :people, through: :person_organizations
end
PersonOrganization < ActiveRecord::Base
belongs_to :person
belongs_to :organization
validates :person, presence: true
validates :organization, presence: true
end
form do |f|
f.inputs do
f.input :name
end
f.inputs 'Organizations' do
f.has_many :person_organizations, do |connection_f|
connection_f.input :organization, as: :select,
collection: Organization.select[:id, :name],
member_label: proc { |org| org.name }
end
end
end
You have to add
accepts_nested_attributes_for :organizations, allow_destroy: true
and if you haven't also the
has_many :person_organizations
in your Person model, and you can place
f.input :organizations, :multiple => true
in your form. Also make sure you permit the correct params in your activadmin register block. In this case it would be
permit_params :name, :organization_ids => []
Read carefully: https://activeadmin.info/5-forms.html#nested-resources and https://activeadmin.info/2-resource-customization.html#setting-up-strong-parameters
I like to decorate multiple select inputs with the select2 javascript library. Let me know if something does not work out.

Rails double nested form with active admin

I have associations as follows:
variant has_may colours and colours has_many sizes. I am using active admin gem for managing backend activities. My models looks like,
class Variant < ApplicationRecord
has_many :variant_colours, dependent: :destroy
accepts_nested_attributes_for : variant_colours, allow_destroy: true
end
class VariantColor < ApplicationRecord
belongs_to :variant
has_many :variant_sizes, dependent: :destroy
accepts_nested_attributes_for :variant_sizes, allow_destroy: true
end
class VariantSize < ApplicationRecord
belongs_to :variant_color
end
It is building variant_colours form with given fields but it is not building variant_sizes form under variant colours. Building meaning it does not populate fields on the form(UI)
form do |f|
f.inputs do
f.input :name
f.input :product
f.input :sku
f.input :stock_quantity
f.inputs do
f.has_many :variant_colors, heading: 'Variant Colors',
allow_destroy: true,
new_record: true do |color_form|
color_form.input :color
color_form.input :sku_code
color_form.input :stock
color_form.inputs do
color_form.has_many :variant_sizes, heading: 'Variant Sizes',
allow_destroy: true,
new_record: true do |size_form|
size_form.input :size
size_form.input :sku_code
size_form.input :stock
end
end
end
end
end
f.actions
end
May not need to wrap f.has_many with anything, so you could try removing one or both of the nested f.inputs and color_form.inputs that you have wrapping your has_many input blocks in the form.
My next thought would be, how are you declaring the permitted params in your controller? They probably need to be something along the lines of:
permit_params :name, :product, :sku, :stock_quantity,
variant_colors_attributes: [
:color, :sku_code, :stock, :_destroy,
# I don't think I've ever tried to double nest in this way, you may need diff syntax
variant_sizes_attributes: [:size, :sku_code, :stock, :_destroy]
]
My guess is the issue is in your permitted params.

Update a relation in activeadmin

I have two models.
fb_page.rb
has_one :fb_page_template, dependent: :destroy
accepts_nested_attributes_for :fb_page_template, :allow_destroy => false
fb_page_template.rb
belongs_to :fb_page
has_many :subscriptions
Active admin
ActiveAdmin.register FbPage do
form title: 'Facebook page form' do |f|
f.has_many :fb_page_template, new_record: false, allow_destroy: false do |k|
k.input :subscribed
end
end
end
Now when I try to update the form, it tries to delete subscriptions and fb_page_template as well.
All I want is to update the value of subscribed which is in fb_page_template
I think that you're missing couple things here:
You need to allow accepts_nested_attributes_for :subscriptions, :allow_destroy => false in your fb_page_template.rb
You need to allow all nested attributes in ActiveAdmin too.
You need to nest the forms.
This is what I have in my fb_pages.rb for ActiveAdmin:
ActiveAdmin.register FbPage do
permit_params :attribute_name_for_fb_page,
fb_page_template_attributes: [
:id, :fb_page_id, :attribute_name_for_fb_page_template,
subscriptions_attributes: [
:subscribed,
:fb_page_template_id
]
]
form title: "Facebook page form" do |f|
f.inputs do
f.input :attribute_name_for_fb_page
f.has_many :fb_page_template, allow_destroy: false do |t|
t.input :attribute_name_for_fb_page_template
t.has_many :subscriptions do |s|
s.input :subscribed, as: :boolean
end
end
end
f.actions
end
end
And this is what I have in the fb_page_template.rb
class FbPageTemplate < ApplicationRecord
belongs_to :fb_page
has_many :subscriptions
accepts_nested_attributes_for :subscriptions, :allow_destroy => false
end
Hope this works for you.

activeadmin has_one select from collection to update related foregn_key

Can't find a solution a week along, please help
Here I have 2 models
Item.rb
class Item < ActiveRecord::Base
has_one :specs_group, class_name: Spec, :dependent => :nullify
attr_accessible :specs_group_attributes
accepts_nested_attributes_for :specs_group, update_only: true
end
Spec.rb
class Spec < ActiveRecord::Base
belongs_to :item
attr_accessible :item_id
end
Here is admin/item.rb
ActiveAdmin.register Item do
form do |f|
f.inputs "Specs group" do
f.has_many :specs_group do |s|
s.input :title, as: :select, collection: Spec.roots.map { |a| [a.title] }
end
end
f.actions
end
end
I want to choose element from Spec collection and update corresponding Spec item. With code above I can choose from dropdown list, but it updates Spec's title or creates new. It's a Rails 3.2 app.

multiple nested has_many in activeadmin

I have the following course.rb model
has_many :chapters
has_many :lectures, through: :chapters
has_many :enrols
has_many :contents, through: :lectures
has_many :users, -> { uniq }, through: :enrols
accepts_nested_attributes_for :chapters, allow_destroy: true
accepts_nested_attributes_for :lectures, allow_destroy: true
and course.rb active admin file
form title: 'Edit Course' do |f|
f.inputs 'Details' do
f.input :course_name
f.input :course_subtitle
f.input :course_description
f.input :video_link
f.input :course_language
f.input :status
f.input :course_image
end
# has_many :contents do |content|
# content.input :description
# content.input :attachment
# end
f.has_many :chapters, allow_destroy: true do |chapter|
# chapter.input :title
chapter.has_many :lectures do |lecture|
# lecture.input :title
# lecture.has_many :contents do |content|
# content.input :description
# end
lecture.input :title
end
end
actions
end
I am trying to make the content of course editable in the course form and it wud require multiple nested has_many since it has number of has_many through relation.
Right now I get undefined methodnew_record?' for nil:NilClass` error
How can it be done? Is there a better way to do it?
undefined method new_record?' for nil:NilClass`
Root cause:
chapter.has_many :lectures do |lecture|
Reason:
You have accepts_nested_attributes_for :lectures, allow_destroy: true in Course model, so you can't have a nested lecture inside Chapter
Solution:
chapter.has_many :lectures do |lecture|
should be
f.has_many :lectures do |lecture|
If you want to have nested lecture inside chapter, then remove accepts_nested_attributes_for :lectures, allow_destroy: true from Course model and add it inside Chapter model.
What if i want to add only the lecture? Your suggestion seem to work
but it gave me option to add lecutre and chapter
In that case, there is no need for f.has_many :chapters, allow_destroy: true do |chapter|. Just have f.has_many :lectures do |lecture|

Resources