简体   繁体   English

ActiveAdmin:如何将Panel添加到Form块中?

[英]ActiveAdmin: how to add a Panel into a Form block?

I have reused a panel for Attachments, that are shown on the show block. 我重新使用了附件的面板,显示在show块上。

I'd like that also be shown on the form block, for Edit. 我希望它也会显示在form块上,用于编辑。

Is that possible? 那可能吗?

I've tried something like this: 我尝试过这样的事情:

form do |f|
  #! attachments_panel:
  panel "Attachments" do
  ul do
    # (..) li components
  end

  #! Editing form
  f.inputs 'Details' do
    f.input :name
    # (..) other inputs
  end

end

But it is only showing the Panel, the form is not included. 但它只是显示小组,表格不包括在内。 Altering the other also shows only the panel. 改变另一个也只显示面板。


EDIT: Based on the solution of @CMaresh, I've created an initializer that monkeypatches the ActiveAdmin gem. 编辑:根据@ CMaresh的解决方案,我创建了一个初始化器,用于monkeypat the ActiveAdmin gem。

#! config/initializers/active_admin_patches.rb
ActiveAdmin::FormBuilder.class_eval do
  def append_panel(&block)
    self.form_buffers.last << Arbre::Context.new({}, self.template, &block)
  end
end

This way, it allows to use the original example code as follows: 这样,它允许使用原始示例代码,如下所示:

form do |f|
  #! attachments_panel:
  f.append_panel do            # <----
    panel "Attachments" do
    ul do
      # (..) li components
    end
  end

  #! Editing form
  f.inputs 'Details' do
    f.input :name
    # (..) other inputs
  end

end

That issue is caused by how the underlying form and Arbre buffers function. 这个问题是由底层形式和Arbre缓冲区如何运作引起的。 The two buffering techniques aren't quite compatible with each other, but to accomplish what you want you can act directly on the form's buffer: 两种缓冲技术彼此不完全兼容,但要实现您想要的功能,您可以直接在表单缓冲区中执行操作:

form do |f|
  f.form_buffers.last << Arbre::Context.new({}, f.template) do
    panel "Panel Above Form" do
      ul do
        li "item 1"
        li "item 2"
      end

      attributes_table_for(f.object) do
        row :name
      end
    end
  end

  f.inputs
end

The Arbre::Context takes two arguments: an assigns hash and a helpers object. Arbre :: Context有两个参数:一个是hash,一个是helpers对象。 It is important to pass in f.template to provide access to all the same helper methods available in views. 传递f.template以提供对视图中可用的所有相同辅助方法的访问非常重要。

An ApplicationHelper 一个ApplicationHelper

A helper could be written to alleviate the noise of creating a new context every time: 可以编写帮助程序以减轻每次创建新上下文的噪音:

# app/helpers/application_helper.rb
module ApplicationHelper
  def append_arbre(f, &block)                                                   
    f.form_buffers.last << Arbre::Context.new({}, f.template, &block)           
  end
end

# app/admin/resource.rb
form do |f|
  append_arbre(f) do
    panel "Panel Above Form" do
      para "Without all the noise."
    end
  end

  f.inputs
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM