ruby-on-rails-3 – 如何处理ActiveAdmin中的多个嵌套资源?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 如何处理ActiveAdmin中的多个嵌套资源?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用带有Rails(3.1.1)的ActiveAdmin(0.4.0).

我找不到一个很好的方式/黑客来处理多个嵌套的资源.

Considerer 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

以下是程序索引,知识和步骤的URL:
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,:class => "edit_knowledge member_link edit_link",:id => "knowledge_#{dom_id(knowledge)}") +\
      link_to("Supprimer",: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 %>

好吧,我被卡住了

如何处理好?任何线索赞赏…

解决方法

那么解决方案很简单
https://github.com/josevalim/inherited_resources
ActiveAdmin.register Step do
  controller do
    nested_belongs_to :program,:knowledge
  end
end

猜你在找的Ruby相关文章