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

时间:2019-06-28 18:36:27

标签: ruby-on-rails activeadmin

我正在尝试针对特定操作自定义active_admin布局。但是我不知道如何向我的自定义布局中添加active_admin的标头?

app / admin / provider.rb 我使用custom_active_admin布局呈现编辑页面

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

我的自定义操作可以正常运行。但是我需要使用默认的active_admin布局将标题添加到首页(如这些页面)。我该怎么办?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在配置初始化程序中注册自定义样式表和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仅应位于该单个视图上,则可以将其包括在视图本身中。

例如,在我拥有的一个应用中,我们在活动管理员的1页上显示图表,并且在视图顶部包括了图表库:

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

%h2 History chart

= line_chart participant.linear_chart_payload, curve: false

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