Nested form in active_admin with select or create option
Asked Answered
S

4

19

We are using active_admin for our administration backend.

We have a model "App" that :belongs_to model "Publisher":

class App < ActiveRecord::Base
  belongs_to :publisher
end

class Publisher < ActiveRecord::Base
  has_many :apps
end

When creating a new entry for the "App" model I want to have the option to either select an existing publisher or (if the publisher is not yet created) to create a new publisher in the same (nested) form (or at least without leaving the page).

Is there a way to do this in active_admin?

Here's what we have so far (in admin/app.rb):

form :html => { :enctype => "multipart/form-data" } do |f|
  f.inputs do
    f.input :title
    ...
  end

  f.inputs do
    f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
      p.input :name
    end
  end

  f.buttons
end

After hours of searching, I'd appreciate any hint... Thanks!

Salas answered 25/8, 2011 at 22:43 Comment(1)
Did you ever manage to do this? I'm trying to do the exact same thing now :-/Bonfire
N
9

First, make sure that in your Publisher model you have the right permissions for the associated object:

class App < ActiveRecord::Base
  attr_accessible :publisher_attributes

  belongs_to :publisher
  accepts_nested_attributes_for :publisher, reject_if: :all_blank
end

Then in your ActiveAdmin file:

form do |f|
  f.inputs do
    f.input :title
    # ...
  end

  f.inputs do
    # Output the collection to select from the existing publishers
    f.input :publisher # It's that simple :)

    # Then the form to create a new one
    f.object.publisher.build # Needed to create the new instance
    f.semantic_fields_for :publisher do |p|
      p.input :name
    end
  end

  f.buttons
end

I'm using a slightly different setup in my app (a has_and_belongs_to_many relationship instead), but I managed to get it working for me. Let me know if this code outputs any errors.

Nobody answered 1/11, 2012 at 22:11 Comment(3)
I tried something equivalent and ended up with an "undefined method `build' for nil:NilClass" using rails 3.1.10 ; am I too far behind ?Landlord
first make sure the relation is set up correctly. In the example above, App belongs to publisher. If you have the relationship type many, you'll have to use publishers instead. Otherwise I guess you can read here #784084 and maybe you'll have to use f.object.build_publisher instead. Let me know if it works, maybe I have to update my answer...Nobody
Can u share code for has_many. I want a selectable list of dropdown publishers for an appBanderilla
T
7

The form_builder class supports a method called has_many.

f.inputs do
  f.has_many :publisher do |p|
    p.input :name
  end
end

That should do the job.

Update: I re-read your question and this only allows to add a new publisher, I am not sure how to have a select or create though.

Timtima answered 6/9, 2011 at 4:55 Comment(0)
A
5

According to ActiveAdmin: http://activeadmin.info/docs/5-forms.html

You just need to do as below:

f.input :publisher
Adulate answered 16/8, 2012 at 9:59 Comment(0)
B
0

I've found you need to do 3 things.

Add semantic fields for the form

f.semantic_fields_for :publisher do |j|
  j.input :name
end

Add a nested_belongs_to statement to the controller

controller do
    nested_belongs_to :publisher, optional: true
end

Update your permitted parameters on the controller to accept the parameters, using the keyword attributes

permit_params publisher_attributes:[:id, :name]
Banna answered 6/1, 2017 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.