Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple nested resources in ActiveAdmin?

I'm using ActiveAdmin (0.4.0) with Rails (3.1.1).

I can't find a nice way/hack to handle multiple nested resources.

Considerer 3 models as:

class Program < ActiveRecord::Base
  has_many :knowledges, :dependent => :destroy
end

class Knowledge < ActiveRecord::Base
  belongs_to :program
  has_many :steps, :dependent => :destroy
end

class Step < ActiveRecord::Base
  belongs_to :knowledge
end

And the ActiveAdmin resources:

ActiveAdmin.register Program do
end

ActiveAdmin.register Knowledge do
  belongs_to :program
end

ActiveAdmin.register Step do
  belongs_to :knowledge
end

In routes.rb:

namespace :admin do
  resources :programs do
    resources :knowledges do
      resources :steps
    end
  end
end

Here's the urls for the index of the programs, the knowledges and the steps :
http://localhost:3000/admin/programs
http://localhost:3000/admin/programs/1/knowledges
http://localhost:3000/admin/programs/1/knowledges/1/steps

No problem for the "Knowledge" admin but the "Step" admin don't keep the nested context.

For example, when I use filters in steps#index I'm redirected to:
http://localhost:3000/admin/knowledges/1/steps?params...
But it must have been:
http://localhost:3000/admin/programs/1/knowledges/1/steps?params...

Same problem when I create a new resource:
http://localhost:3000/admin/knowledges/1/steps/new
Instead of:
http://localhost:3000/admin/programs/1/knowledges/1/steps/new

Same problem with the breadcrumb... etc...

What I've tried so far in app/admin/steps.rb:

ActiveAdmin.register Step do

  belongs_to :knowledge

  config.clear_action_items!
  action_item :only => :index do
    link_to('Create Step', new_admin_program_knowledge_step_path(knowledge.program.id, knowledge.id))
  end

  index do
    column :id
    column :knowledge
    column :title
    column "Actions" do |step|
      link_to("Voir", admin_program_knowledge_step_path(step.knowledge.program, step.knowledge, step), :class => "member_link show_link") +\
      link_to("Editer", edit_admin_program_knowledge_step_path(step.knowledge.program, step.knowledge, step), :class => "edit_knowledge member_link edit_link", :id => "knowledge_#{dom_id(knowledge)}") +\
      link_to("Supprimer", admin_program_knowledge_step_path(step.knowledge.program, step.knowledge, step), :class => "member_link delete_link", :method => :delete, :confirm => "Delete?")
    end
  end

  filter :id
  filter :title
  filter :subtitle
  filter :stage_type
  filter :order_by
  filter :created_at
  filter :updated_at

  form :partial => "form"

end

And in app/views/admin/steps/_form.html.erb I must use the activeadmin formbuilder:

<%= semantic_form_for(resource, :url => admin_program_knowledge_steps_path(resource.knowledge.program, resource.knowledge), :builder => ActiveAdmin::FormBuilder) do |f|
  f.inputs "Step" do
    f.input :knowledge, :as => :hidden
    f.form_buffers.last << f.template.content_tag(:li, f.template.content_tag(:label, "Knowledge")+f.template.content_tag(:p, f.object.knowledge.title))
    f.input :title
    f.input :order_by
  end
  f.buttons
end %>

Well... I'm stuck.

How to handle this nicely? Any clues appreciated...

like image 741
fro_oo Avatar asked Feb 05 '12 14:02

fro_oo


1 Answers

Well, the solution is pretty simple... https://github.com/josevalim/inherited_resources

ActiveAdmin.register Step do
  controller do
    nested_belongs_to :program, :knowledge
  end
end
like image 116
fro_oo Avatar answered Sep 28 '22 02:09

fro_oo