如何将 active_admin header 注入自定义布局?

How to inject active_admin header into custom layout?

我正在尝试为特定 action.But 自定义 active_admin 布局 我不知道如何将 active_admin 的 header 添加到我的自定义布局中?

app/admin/provider.rb 我使用 custom_active_admin layout

呈现编辑页面
controller do
 def edit
   @provider = Provider.find(params[:id])
   render 'admin/providers/edit', layout: 'custom_active_admin'
 end
 ...
end

app/views/admin/providers/edit.html.haml

= semantic_form_for ...
  = f.inputs 'Location', for: :location do |location|
    = location.input :postal_code
    = location.input :prefecture

app/views/layouts/custom_active_admin.html.haml 我正在尝试为我的特殊操作自定义布局

!!!
%html
  %head
    = csrf_meta_tags
    = csp_meta_tag
    ...
    = stylesheet_link_tag 'custom_active_admin', media: 'all'
    = javascript_include_tag 'custom_active_admin'

  %body.new.active_admin.logged_in.admin_namespace
    #header
      = yield :head
    .wrapper.without_sidebar#active_admin_content
      .main_content_wrapper
        .alert-message-container
        = yield

我的自定义操作可以正常工作。但我需要将 header 添加到首页,就像那些使用默认 active_admin 布局的页面一样。我该怎么做?谢谢。

您可以在配置初始值设定项中注册自定义样式表和 javascript。我建议只将 CSS 和 JS 添加到您的活动管理配置中:

# config/initializers/active_admin.rb

# == Register Stylesheets & Javascripts
#
# We recommend using the built in Active Admin layout and loading
# up your own stylesheets / javascripts to customize the look
# and feel.
#
# To load a stylesheet:
   config.register_stylesheet 'my_stylesheet.css'
#
# You can provide an options hash for more control, which is passed along to stylesheet_link_tag():
#   config.register_stylesheet 'my_print_stylesheet.css', media: :print
#
# To load a javascript file:
   config.register_javascript 'my_javascript.js'

如果您强烈认为 JS 应该只在那个单一视图上,您​​可以将它包含在视图本身中。

例如,在我的一个应用程序中,我们在活动管理员的一页上显示图表,并在视图顶部包含图表库:

= javascript_include_tag "//www.google.com/jsapi", "chartkick"

%h2 History chart

= line_chart participant.linear_chart_payload, curve: false

脚本进入正文,这并不理想,但效果很好。我认为不需要将 CSS 放在视图本身中,因为您应该能够使用选择器来确保 CSS 仅适用于您需要的视图。