【问题标题】:Active Admin: How to set page title?活动管理员:如何设置页面标题?
【发布时间】:2013-06-03 20:51:53
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activeadmin


    【解决方案1】:

    合并答案并补充一点:

    大部分内容都在this page on the 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
    

    【讨论】:

      【解决方案2】:

      搜索了下,

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

      例如

      1) 为索引页设置标题,

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

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

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

      【讨论】:

      【解决方案3】:

      如果有人(比如我)仍然在为行动而苦恼new

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

      别忘了加super。但是,edit 操作不需要。

      【讨论】:

        【解决方案4】:

        根据this post,您可以在选择的操作中使用如下行:

        @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
        

        【讨论】:

        • 带有def new do块的方法定义你确定吗?
        【解决方案5】:

        如果我的问题正确,您想在 activeadmin 中重命名模型以在所有操作中以另一个名称出现,例如重命名“Post”模型以在 ActiveAdmin 中显示“Article”,这样做只需转到模型Admin文件夹中的文件,第一行从

        ActiveAdmin.register Post do
        

        ActiveAdmin.register Post, as: "Article"
        

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

        【讨论】:

          【解决方案6】:

          我的例子:

          两个模型及其关联:

          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
          

          【讨论】:

            【解决方案7】:

            简单地做

            index title: "Me new title"
            

            【讨论】:

              【解决方案8】:

              如果您使用的是ActiveAdmin.register_page,并且想在顶部设置菜单名称和页面名称,请尝试以下操作:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多