Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do deep nesting in active admin?

It's the third day I'm crushing on Active Admin.

I have @survey that has_many :questions and each question has_many :answers - they are actually variants users can choose from.

But still I cant put it to work, it just doesn't create anything deeper then 1 level: even the form works properly, but nothing is created.

like image 409
prikha Avatar asked Nov 22 '11 11:11

prikha


1 Answers

I have the following clases Course->Sections->Lessons.

I did the following:

form do |f|
  f.inputs "Details" do
    f.input :instructor, :as => :select 
    f.input :title
    f.input :name
    f.input :price
    f.input :discount
    f.input :slug
    f.inputs "Sections" do
       f.has_many :sections, :header=>"" do |section|
         section.input :name
         section.input :position
         if section.object.id
           section.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
         end

         section.has_many :lessons, :header=>"Lessons" do |lesson|
           lesson.input :title
           lesson.input :position
           lesson.input :duration
           lesson.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
         end
       end
   end

  end
  f.buttons
end

My models are as follow:

class Course < ActiveRecord::Base
    has_many :sections, :dependent => :delete_all 
    accepts_nested_attributes_for :sections, :allow_destroy => true
    attr_accessible :sections_attributes
 ....

class Section < ActiveRecord::Base
    belongs_to :course
    has_many :lessons, :dependent => :delete_all
    attr_accessible :course_id, :name, :position
    accepts_nested_attributes_for :lessons, :allow_destroy => true
    attr_accessible :lessons_attributes
....

class Lesson < ActiveRecord::Base
    belongs_to :section
    attr_accessible :duration, :position, :section_id, :title
....

And it works great! I don't know what happens if I go more levels deeper.

like image 121
Tony Avatar answered Sep 27 '22 20:09

Tony