ActiveAdmin forms with has_many - belongs_to relationships?

26,289

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)

Share:
26,289
agente_secreto
Author by

agente_secreto

Updated on March 20, 2020

Comments

  • agente_secreto
    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
    agente_secreto over 12 years
    Great, I will check them out, railscasts are always very useful
  • agente_secreto
    agente_secreto over 12 years
    Thanks man, but as soon as I use f.has_many I get an error: undefined method `new_record?' for nil:NilClass
  • agente_secreto
    agente_secreto over 12 years
    Maybe it has anything to do with the polymorphic association?
  • agente_secreto
    agente_secreto over 12 years
    I tried using accepts_nested_attributes_for :photos in my Home model, but that doesnt work either.
  • agente_secreto
    agente_secreto over 12 years
    Ok, 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
    Agis over 11 years
    I also get the undefined method 'new_record?' for nil:NilClass error. How did you solved it @agente_secreto?
  • Ildar
    Ildar over 11 years
    You need to add accepts_nested_attributes_for :children in your Parent model - that's how it solved
  • jfedick
    jfedick about 11 years
    @agente_secreto I updated my solution with the accepts_nested_attributes call.
  • ScottJShea
    ScottJShea almost 11 years
    I had to do f.input :name rather than f.name.
  • tmimicus
    tmimicus over 10 years
    Also need to add f.actions as the last item in the form block to get the 'Create' and 'Cancel' buttons
  • Малъ Скрылевъ
    Малъ Скрылевъ almost 10 years
    Will the engine allow to update each photo's data separately without a whole page reload?
  • rahulrvp
    rahulrvp over 6 years
    if 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.