2

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 !

6
  • I'm not sure, but you can try instead of cetb.inputs to do cetb.has_one :addresscommunication Commented Jan 26, 2015 at 17:07
  • Although that would make perfect sense, it's unfortunately not the way AA handles 1:1 relationships in forms, Andrey Commented Jan 26, 2015 at 17:40
  • Aborting with abort (a.input :adresse1).inspect in the for loop gives : Commented Jan 28, 2015 at 8:49
  • "<li class=\"string input optional stringish\" id=\"eleve_representants_attributes_NEW_REPRESENTANT_RECORD_adressecommunication_attributes_adresse1_input\"><label class=\"label\" for=\"eleve_representants_attributes_NEW_REPRESENTANT_RECORD_adressecommunication_attributes_adresse1\">Etage, couloir, escalier, n° d’appartement</label><input id=\"eleve_representants_attributes_NEW_REPRESENTANT_RECORD_adressecommunication_attributes_adresse1\" maxlength=\"250\" name=\"eleve[representants_attributes][NEW_REPRESENTANT_RECORD][adressecommunication_attributes][adresse1]\" type=\"text\" />\n\n</li>" Commented Jan 28, 2015 at 8:49
  • possible related github.com/activeadmin/activeadmin/issues/3579 Commented Jan 30, 2015 at 0:58

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.