活跃管理员:如何设置页面标题?

问题描述 投票:0回答:9

这看起来应该相对简单,但我在寻找答案时遇到了一些麻烦:

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

ruby-on-rails activeadmin
9个回答
38
投票

合并答案并添加一点:

大部分内容都在 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

10
投票

搜索后找到了,

您可以将 :title 属性添加到活动管理块中。

例如

1) 设置索引页标题,

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

2) 设置显示页面的标题,

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

5
投票

根据这篇文章,您可以在选择的操作中使用如下行:

@page_title="My Custom Title"

例如,要在“新”等预先存在的操作中实现此操作,您可以执行以下操作:

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

4
投票

万一有人(像我一样)仍然在行动上挣扎

new

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

别忘了添加

super
。然而,
edit
行动不需要这样。


4
投票

如果我的问题正确,您想要在 ActiveAdmin 中重命名模型以在所有操作中以其他名称显示,例如将“Post”模型重命名为在 ActiveAdmin 中显示“Article”,只需转到其中的模型文件即可管理文件夹和第一行更改为

ActiveAdmin.register Post do

ActiveAdmin.register Post, as: "Article"

如果出现问题,请重新启动 Rails 服务器、docker 或其他任何东西


3
投票

我的例子:

两个模型及其关联:

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

2
投票

如果您使用

ActiveAdmin.register_page
并且想要将菜单名称和页面名称设置在顶部,请尝试以下操作:

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

1
投票

还可以利用控制器操作来更精细地更改页面标题。 特别是当像 @dayudodo 在这个

Answer
中那样利用 I18n 和 before_action 时。

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"

没有 I18n

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
    

0
投票
简单做

index title: "Me new title"
    
© www.soinside.com 2019 - 2024. All rights reserved.