Skip to content

Set page title

victorpolko edited this page · 10 revisions

If you'd like to override the page titles, follow this pattern (from #184)

ActiveAdmin.register Post do
  # Custom string
  index :title => 'Awesome Title' do
    # ...
  end
  # Call a method
  show :title => :my_title_method do
    # ...
  end
  # execute some code
  show :title => proc{|post| post.title } do
     # ...
  end
  # Custom actions
  collection_action :comments, :title => "My Awesome Comments" do
    # do stuff
  end
end

However, this doesn't yet handle the edit page because the form method ignores the :title. For edit pages, do this instead:

ActiveAdmin.register Post do
  controller do
    def edit
      @page_title = "Awesome Title"
    end
  end
end

Or, for all pages at once do this (from #2891):

ActiveAdmin.register Post do
  controller do
    before_filter { @page_title = "#{current_user} Posts" }
  end
end

Image or link in the title bar of a custom page

It is also possible to set the picture/link or whatever as the title of the page:

ActiveAdmin.register_page 'CustomPage' do
  content title: Proc.new {
    link_to image_tag('some_logo.png'), 'http://someurl.com/', target: '_blank'
  } do
    ... # content here
  end
end

However, this will break the <title> tag as it is assembled from the same 'title' block which is passed to content and the site_name variable. This is what the page title will look like in browser:

<a href="http://someurl.ru/" target="_blank"><img alt="Some logo" src="/assets/some_logo.png" /></a> | SiteName

There is a small hack for this case, based on the order of assembly in ActiveAdmin::Views::Pages::Base#build_page:

module ActiveAdmin
  module Views
    module Pages
      class Base < Arbre::HTML::Document
      ...
      private

      def build_page
        within @body do
          div id: "wrapper" do
            build_unsupported_browser
            build_header # <title> tag comes first
            build_title_bar # then comes the title bar
            build_page_content
            build_footer
          end
        end
      end
      ...

So this is a solution (@victorpolko):

ActiveAdmin.register_page 'CustomPage' do
  content title: Proc.new {
    if @page_title.present? # In other words, we check whether the <title> tag was already rendered
      link_to image_tag('some_logo.png'), 'http://someurl.com/', target: '_blank'
    else
      @page_title = 'CustomPage' # @page_title is just a custom-named instance variable, it could be anything
    end
  } do
    ... # content here
  end
end
Something went wrong with that request. Please try again.