如何在ActiveAdmin中处理多个嵌套资源?

时间:2012-02-05 14:40:26

标签: ruby-on-rails-3 activeadmin

我正在使用带有Rails(3.1.1)的ActiveAdmin(0.4.0)。

我找不到一个很好的方法/ hack来处理多个嵌套资源。

考虑3个模型:

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

和ActiveAdmin资源:

ActiveAdmin.register Program do
end

ActiveAdmin.register Knowledge do
  belongs_to :program
end

ActiveAdmin.register Step do
  belongs_to :knowledge
end

在routes.rb中:

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

以下是节目索引,知识和步骤的网址:
http://localhost:3000/admin/programs
http://localhost:3000/admin/programs/1/knowledges
http://localhost:3000/admin/programs/1/knowledges/1/steps

“知识”管理员没有问题,但“步骤”管理员不保留嵌套上下文。

例如,当我在步骤#index中使用过滤器时,我被重定向到:
http://localhost:3000/admin/knowledges/1/steps?params ...
但一定是:
http://localhost:3000/admin/programs/1/knowledges/1/steps?params ......

创建新资源时遇到同样的问题:
http://localhost:3000/admin/knowledges/1/steps/new
而不是:
http://localhost:3000/admin/programs/1/knowledges/1/steps/new

面包屑......等同的问题......

到目前为止我在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

在app / views / admin / steps / _form.html.erb中我必须使用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 %>

嗯......我被卡住了。

如何妥善处理?任何线索都赞赏......

1 个答案:

答案 0 :(得分:4)

嗯,解决方案非常简单...... https://github.com/josevalim/inherited_resources

ActiveAdmin.register Step do
  controller do
    nested_belongs_to :program, :knowledge
  end
end