11

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...

1 Answer 1

4

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

3 Comments

This doesn't seem to work with rails 3.2.6 and active_admin 0.4.4
To bad... maybe ask Jose Valim about the correct way to handle this with the new version of AA... and post the solution here. Thx
It works great in the latest Rails 4.1 and ActiveAdmin 1.0pre (from master). Thank you :).

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.