gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:23:39 29 4
gpt4 key购买 nike

我在 Rails (3.1.1) 中使用 ActiveAdmin (0.4.0)。

我找不到处理多个嵌套资源的好方法/技巧。

考虑者 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

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

例如,当我在 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 %>

嗯……我被卡住了。

如何很好地处理这个问题?任何线索表示赞赏...

最佳答案

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

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

关于ruby-on-rails-3 - 如何在 ActiveAdmin 中处理多个嵌套资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150085/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com