Git Psuh
Git Psuh

Reputation: 322

Active Admin has_one association in nested form not displayed

The application I'm working on has just been updated today from Rails 4.0 to Rails 4.1 because some guys needed a newer version of Active Admin. The piece of code below, which was working with the previous version of AA, is not displayed anymore, for some reason.

I've got a form embedding a has_one relationship that in turn has another has_one relationship, so here is how it looks:

form do |f|
#some code
f.inputs "Chef d'établissement", for: [:chef_etablissement, f.object.chef_etablissement || ChefEtablissement.new] do |cetb|
    cetb.input :civilite_gabriel_id, label: "Civilité", :as => :select,
      :collection => Civilite.all.map{|c| ["#{c.libelle_long.capitalize}", c.id_gabriel]},
      include_blank: false
    cetb.input :nom_patro
    cetb.input :nom
    cetb.input :prenom_usuel
    cetb.input :mail1
    cetb.inputs "Adresse Cetb", for: [:adressecommunication, cetb.object.adressecommunication || Adressecommunication.new] do |adrcetb|
        adrcetb.input :adresse1
        adrcetb.input :adresse2
        adrcetb.input :adresse4, label: "Type de voie"
        adrcetb.input :adresse3
        adrcetb.input :adresse5, label: "Mention"
        adrcetb.input :code_postal
        adrcetb.input :ville
      end
    end

I reckon this has something to do with the "cetb.inputs" part overwriting my previous "cetb.input" instructions since when I do comment my "cetb.input" lines, the form appears but I can't really figure out a way of doing it the proper way. I mean, putting the "cetb.input" lines in the "cetb.inputs for:" part would make it all displayed but it's not really what I'm looking for since I'd have a useless fieldset and that would look rather ugly.

Any hints welcome, thanks !

Upvotes: 2

Views: 3432

Answers (3)

hcarreras
hcarreras

Reputation: 4612

For those using the version 1.0, the documentation was pretty useful: https://github.com/activeadmin/activeadmin/wiki/Nested-model-form-image-upload

form :html => { :multipart => true } do |f|
  f.inputs "General" do
    f.input :name
  end

  f.inputs "Image or Video", :for => [:media, f.object.media || Media.new ] do |fm|
    fm.inputs "Image", :for => [:image, fm.object.image || Image.new] do |fmi|
      fmi.input :file, :for => :image, :as => :file, :hint => f.template.image_tag(f.object.media.image.url(:cropped))
    end
    fm.inputs "Video", :for => [:video, fm.object.video || Video.new] do |fmv|
      fmv.input :url
    end
  end
end

Upvotes: 3

user2168130
user2168130

Reputation: 291

So, I remember doing this exact thing not too long ago, and DO NOT remember having such a hard time doing it. However, this is how I solved the exact problem that you were having.

I tried @Nick M's answer, but that gave me "Remove" and "Add Color" buttons below my nested resource, which I DO NOT want.

Hope this helps someone.

form do |f|
  if f.object.new_record?
    f.object.build_color
  end

  f.inputs do
    f.input :title
    f.input :overview

    f.inputs "Color" do 
      f.semantic_fields_for :color do |color| 
        color.input :hex_code
      end 
    end

  end
  f.actions
end

Upvotes: 0

Nick M
Nick M

Reputation: 279

We ran into the same issue after a recent ActiveAdmin upgrade, and this issue held the key: https://github.com/activeadmin/activeadmin/issues/3791 We had to switch from the f.inputs way of calling the nested form to use f.has_many (which, as noted, is actually valid for has_one relationships).

So would this approach work for you?

cetb.object.build_adressecommunication unless(cetb.object.adressecommunication)
cetb.has_many :adressecommunication do |adrcetb|
  adrcetb.input :adresse1
  adrcetb.input :adresse2
  adrcetb.input :adresse4, label: "Type de voie"
  adrcetb.input :adresse3
  adrcetb.input :adresse5, label: "Mention"
  adrcetb.input :code_postal
  adrcetb.input :ville
end

Upvotes: 0

Related Questions