ActiveAdmin forms with has_many - belongs_to relationships?
Solution 1
Try something like this in app/admin/home.rb:
form do |f|
f.inputs "Details" do
f.name
end
f.has_many :photos do |photo|
photo.inputs "Photos" do
photo.input :field_name
#repeat as necessary for all fields
end
end
end
Make sure to have this in your home model:
accepts_nested_attributes_for :photos
I modified this from another stack overflow question: How to use ActiveAdmin on models using has_many through association?
Solution 2
You could try this:
form do |f|
f.semantic_errors # shows errors on :base
f.inputs # builds an input field for every attribute
f.inputs 'Photos' do
f.has_many :photos, new_record: false do |p|
p.input :field_name
# or maybe even
p.input :id, label: 'Photo Name', as: :select, collection: Photo.all
end
end
f.actions # adds the 'Submit' and 'Cancel' buttons
end
Also, you can look at https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md (See Nested Resources)
agente_secreto
Updated on March 20, 2020Comments
-
agente_secreto about 4 years
I have the models Home and Photo, which have a has_many - belongs_to relationship (a polymorphic relationship, but I dont think that matters in this case). I am now setting up active admin and I would like admins to be able to add photos to homes from the homes form.
The photos are managed by the CarrierWave gem, which I dont know if will make the problem easier or harder.
How can I include form fields for a different model in the Active Admin Home form? Any experience doing something like this?
class Home < ActiveRecord::Base validates :name, :presence => true, :length => { :maximum => 100 } validates :description, :presence => true has_many :photos, :as => :photographable end class Photo < ActiveRecord::Base belongs_to :photographable, :polymorphic => true mount_uploader :image, ImageUploader end
-
agente_secreto over 12 yearsGreat, I will check them out, railscasts are always very useful
-
agente_secreto over 12 yearsThanks man, but as soon as I use f.has_many I get an error: undefined method `new_record?' for nil:NilClass
-
agente_secreto over 12 yearsMaybe it has anything to do with the polymorphic association?
-
agente_secreto over 12 yearsI tried using accepts_nested_attributes_for :photos in my Home model, but that doesnt work either.
-
agente_secreto over 12 yearsOk, I did that last part wrong the first time around, but that was the remaining problem, so maybe you should add it to your answer. Thanks!
-
Agis over 11 yearsI also get the undefined method 'new_record?' for nil:NilClass error. How did you solved it @agente_secreto?
-
Ildar over 11 yearsYou need to add accepts_nested_attributes_for :children in your Parent model - that's how it solved
-
jfedick about 11 years@agente_secreto I updated my solution with the accepts_nested_attributes call.
-
ScottJShea almost 11 yearsI had to do
f.input :name
rather thanf.name
. -
tmimicus over 10 yearsAlso need to add
f.actions
as the last item in theform
block to get the 'Create' and 'Cancel' buttons -
Малъ Скрылевъ almost 10 yearsWill the engine allow to update each photo's data separately without a whole page reload?
-
rahulrvp over 6 yearsif I have a set of images and I want to select the values from them, how can I do it using forms? ex: I have a product and several categories when I create a new product I should be able to add multiple categories from a predefined list of categories.