在没有模型的情况下,使用 ActiveAdmin 添加自定义控制器可以通过以下步骤实现:
app/controllers/custom_controller.rb
:class CustomController< ApplicationController
def index
# 在这里编写自定义控制器逻辑
end
end
config/routes.rb
文件中添加路由指向自定义控制器:Rails.application.routes.draw do
get 'custom', to: 'custom#index'
end
ActiveAdmin.register_page "CustomController" do
menu label: "Custom Controller", parent: "Dashboard"
content do
link_to "Go to Custom Controller", "/custom"
end
end
app/admin/dashboard.rb
文件中添加菜单项:ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc { I18n.t("active_admin.dashboard") }
content title: proc { I18n.t("active_admin.dashboard") } do
# 在这里添加自定义内容
end
page_action :custom_controller, method: :get do
redirect_to "/custom"
end
end
现在,在 ActiveAdmin 的仪表板中,可以看到一个名为 "Custom Controller" 的菜单项,点击它将跳转到自定义控制器的页面。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云