简体   繁体   中英

Active Admin: How to set page title?

This seems like it should be relatively simple, but I've had some trouble tracking down the answer:

How do you set the page title in ActiveAdmin?

Consolidating answers and adding a little:

Most of this is on this page on the wiki (or I'll put it there soon).

Within the file that registers your model for activeadmin (eg app/admin/user.rb), you can have

ActiveAdmin.register User do
  # a simple string
  index :title => "Here's a list of users" do
    ...
  end

  # using a method called on the instance of the model
  show :title => :name do
    ...
  end

  # more flexibly using information from the model instance
  show :title => proc {|user| "Details for "+user.name } do
    ...
  end

  # for new, edit, and delete you have to do it differently
  controller do
    def edit
      # use resource.some_method to access information about what you're editing
      @page_title = "Hey, edit this user called "+resource.name
    end
  end
end

After searching got it,

You can add :title attribute to blocks of active admin.

eg

1) To set title for index page,

index :title => 'Your_page_name' do
....
end

2) To set title for show page,

show :title => 'Your_page_name' do
....
end

In case someone (like me) still struggles with action new :

def new
  @page_title="My Custom Title"
  super
end

Dont forget to add super . However, edit action doesnt need that.

As per this post , you can use a line like the following in the action of choice:

@page_title="My Custom Title"

For example, to implement this in a pre-existing action like 'new', you would do something like this:

controller do
  def new do
    @page_title="My Custom Title"
    new! do |format|
       format.html{render "my_new"}
    end
  end
end

if I got your question right, you want to rename a model in activeadmin to appear with another name in all actions, for example renaming "Post" model to appear "Article" in ActiveAdmin, to do so just go to the model file inside Admin folder and the first line change from

ActiveAdmin.register Post do

to

ActiveAdmin.register Post, as: "Article"

and if something went wrong then restart your rails server, docker or whatever it is

简单地做

index title: "Me new title"

My example:

two models and their association :

class Course < ApplicationRecord
    has_many :lessons, dependent: :destroy
end
class Lesson < ApplicationRecord
  belongs_to :course
end

I want to show the course title in its lessons index page, I tested two ways to do that.

just for index

ActiveAdmin.register Lesson do
  belongs_to :course, optional: true
  permit_params :title
  controller do
    def index
      @page_title = " #{Course.find(params[:course_id]).try(:title)}"
      super
    end
end

or for all actions

ActiveAdmin.register Lesson do
  belongs_to :course, optional: true
  permit_params :title
  controller do
    before_action {  
      @page_title = " #{Course.find(params[:course_id]).try(:title)}"
    }
  end
end

then you can use the @page_title

  index :title=> @page_title  do
    selectable_column
    id_column
    column :title
    actions

  end

If you are using ActiveAdmin.register_page and want to set the menu name and page name at the top, try the following:

ActiveAdmin.register_page "ReleaseNotes" do
  menu label: "Release Notes"
  
  content title: "Release Notes" do
    render partial: 'index'
  end
end

It is possible to also leverage the controller action to give more granularity to changing the page title. Especially when leveraging I18n with the before_action like @dayudodo did in this Answer .

I18n approach

ActiveAdmin.register User do
  controller do
    before_action :page_title

    # other controller stuff...

    def page_title
      @page_title = I18n.t(params[:action], scope: "active_admin.resources.user.page_title")
    end

    # other controller stuff...
  end
end

I have copied the active_admin yml locally as suggested by ActiveAdmin's documentation so that I can modify it for customization

en:
  # other yaml stuff...

  active_admin:
    # more active_admin yaml stuff...

    resources: # added this key for organizing the customization
      # more resources...
      
      user:
        page_title:
          index: Custom Index Title
          new: Custom New Title

  # other yaml stuff...

with this you can still leverage injected variables with I18n, assuming you have method in your controller to like:

  def user
    @user ||= User.find(params[:user_id])
  end

  def page_title
    @page_title = I18n.t(params[:action], scope: "active_admin.resources.user.page_title", name: user.name)
  end 
  user:
    page_title:
      index: Custom Index Title
      new: Custom New Title
      show: "%{name}'s Details"
      edit: "Edit %{name}'s Details"

without I18n

constants in ActiveAdmin blocks violate Ruby Style guides and will throw linter errors: Defining Constants within a Block . So we can memoize instead

ActiveAdmin.register User do
  controller do
    before_action :page_title

    # other controller stuff...

    def action
      params[:action].to_sym
    end

    def page_title
      @page_title = page_titles_per_action[action]
    end

    def page_titles_per_action
      @page_titles_per_action ||= { index: "Custom Index Title", new: "Custom New Title" }
    end

    # other controller stuff...
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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