Active Admin:如何设置页面标题?

29

这似乎应该相对简单,但我在查找答案时遇到了一些麻烦:

如何在ActiveAdmin中设置页面标题?

9个回答

37

整理答案并稍作补充:

大部分内容可以在wiki页面上找到(或者我很快会放到那里)。

在注册你的模型以供activeadmin使用的文件内(例如app/admin/user.rb),你可以添加以下内容:

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

9

搜索到相应内容后,

您可以为Active Admin的块添加:title属性。

例如:

1)为索引页面设置标题,

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

2) 设置展示页面的标题:

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

2
请设置页面标题,详情请查看:https://github.com/gregbell/active_admin/wiki/Set-page-title - bunty

5
根据这篇文章,您可以在所选操作的动作中使用以下行:
@page_title="My Custom Title"

例如,在像“new”这样的预先存在的操作中实现此功能,您可以执行以下操作:
controller do
  def new do
    @page_title="My Custom Title"
    new! do |format|
       format.html{render "my_new"}
    end
  end
end

方法定义与块def new do,确定吗? - Nishutosh Sharma

4

如果有人(比如我)仍然在处理new操作时遇到困难:

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

不要忘记添加super。然而,edit操作不需要它。

4

如果我理解正确,您希望在ActiveAdmin中重命名模型以在所有操作中显示另一个名称,例如将“Post”模型重命名为“Article”,要实现此目的,请进入Admin文件夹内的模型文件,并将第一行更改为:

ActiveAdmin.register Post do

为了

ActiveAdmin.register Post, as: "Article"

如果出现问题,那么重新启动您的Rails服务器、Docker或其他任何相关工具。


2

我的例子:

两个模型及其关联:

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

我希望在课程索引页中显示课程标题,我尝试了两种方法。

仅适用于索引页

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

或适用于所有操作

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

那么你可以使用 "@page_title"。

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

  end

1
如果您正在使用ActiveAdmin.register_page并希望在顶部设置菜单名称和页面名称,请尝试以下操作:
ActiveAdmin.register_page "ReleaseNotes" do
  menu label: "Release Notes"
  
  content title: "Release Notes" do
    render partial: 'index'
  end
end

0

只需简单地执行

index title: "Me new title"

0

可以利用控制器操作来更细致地改变页面标题。特别是在使用I18n和before_action时,就像@dayudodo在Answer中所做的那样。

I18n方法

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

我已经按照ActiveAdmin的文档建议,将active_admin yml本地复制,以便我可以修改它进行定制。

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...

通过这个,您仍然可以使用注入的变量与I18n一起使用,假设您在控制器中有像这样的方法:

  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"

没有国际化

在ActiveAdmin块中定义常量会违反Ruby风格指南并引发linter错误:在块内定义常量。因此,我们可以使用memoize

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接