| redirect_from | /docs/10-custom-pages.html |
|---|
If you have data you want on a standalone page that isn't tied to a resource, custom pages provide you with a familiar syntax and feature set:
- a menu item
- sidebars
- action items
- page actions
Creating a page is as simple as calling register_page:
# app/admin/calendar.rb
ActiveAdmin.register_page "Calendar" do
content do
para "Hello World"
end
endAnything rendered within content will be the main content on the page.
Partials behave exactly the same way as they do for resources:
# app/admin/calendar.rb
ActiveAdmin.register_page "Calendar" do
content do
render partial: 'calendar'
end
end
# app/views/admin/calendar/_calendar.html.arb
table do
thead do
tr do
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].each &method(:th)
end
end
tbody do
# ...
end
endSee the Menu documentation.
ActiveAdmin.register_page "Calendar" do
breadcrumb do
['admin', 'calendar']
end
endWe use the admin namespace by default, but you can use anything:
# Available at /today/calendar
ActiveAdmin.register_page "Calendar", namespace: :today
# Available at /calendar
ActiveAdmin.register_page "Calendar", namespace: falseTo nest the page within another resource, you can use the belongs_to method:
ActiveAdmin.register Project
ActiveAdmin.register_page "Status" do
belongs_to :project
endSee also the Belongs To documentation and examples.
See the Sidebars documentation.
Just like other resources, you can add action items. The difference here being that
:only and :except don't apply because there's only one page it could apply to.
action_item :view_site do
link_to "View Site", "/"
endPage actions are custom controller actions (which mirror the resource DSL for the same feature).
page_action :add_event, method: :post do
# ...
redirect_to admin_calendar_path, notice: "Your event was added"
end
action_item :add do
link_to "Add Event", admin_calendar_add_event_path, method: :post
endThis defines the route /admin/calendar/add_event which can handle HTTP POST requests.
Clicking on the action item will reload page and display the message "Your event was added"
Page actions can handle multiple HTTP verbs.
page_action :add_event, method: [:get, :post] do
# ...
endSee also the Custom Actions example.
You can use custom parameter instead of id
ActiveAdmin.register User do
controller do
defaults :finder => :find_by_name
end
endThis defines the resource route as /admin/users/john if user name is john