Skip to main content

Tabs

tab adds a tab to the show page. A tab with an empty action name ("") holds the record's own fields; any other tab gets its own route (/:id/<action_name>) and usually lists an association.

Quick example​

cm_show page_title: :title do
tab :profile, "" do
cm_section "Details" do
field :title
field :status, field_type: :badge, badge_class: STATUS_TAG_COLOR
end
end

tab :comments, "comment", associated_model: "comments", layout_type: "cm_association_index" do
column :message
end
end

Options​

tab(tab_name, action_name, associated_model: nil, layout_type: nil, layout: nil, partial: nil, display_if: nil, show_count: true, view_type: nil, associated_model_name: nil, allow_create_action: -> { true }, eager_load_associations: nil, relationship_type: :has_many, &block)
NameTypeDefaultDescription
tab_nameString | SymbolrequiredTab label.
action_nameStringrequired"" for the main tab; otherwise a unique action name used in routes and permissions.
associated_modelString | SymbolnilAssociation on the record to list (e.g. "comments").
layout_typeStringnilcm_association_index (list) or cm_association_show (single record).
layoutStringnilLayout of the tab.
partialStringnilRender this partial as the tab content.
display_ifProcnilLambda returning whether to show the tab.
view_typeSymbolnil:table or :card for association lists.
show_countBooleantrueShow the record count badge.
associated_model_nameStringinferredClass name of the associated model when it differs from the association.
allow_create_actionProc-> { true }Whether the Add button is shown in the tab.
eager_load_associationsArraynilAssociations to preload for the listed records.
relationship_typeSymbol:has_many:has_many or :has_one. See Has One Tab.
note

After adding an association tab, enable its permission for each role in Roles β†’ Permissions.

Recipes​

List an association in a tab​

Pass the association name as associated_model and declare its columns β€” the tab renders an index-style table of the associated records, scoped to the parent:

cm_show page_title: :title do
tab :applications, "job_applications", associated_model: :job_applications,
layout_type: "cm_association_index", associated_model_name: "JobApplication" do
column :user_full_name, header: "Applicant"
column :status, field_type: :badge, badge_class: STATUS_TAG_COLOR
column :created_at, field_type: :datetime, format: "%d %b, %Y %H:%M", header: "Applied At"
end
end

Use associated_model_name when the class can't be inferred from the association name, view_type: :card for a card grid, show_count: false to hide the count badge, and eager_load_associations: %i[user] to avoid N+1 queries in the list.

Render a partial as the tab content​

For content that doesn't fit fields or columns β€” charts, custom markup β€” point partial at a view:

tab :permissions, "permissions", layout_type: "cm_association_show",
partial: "/cm_admin/roles/permissions"

The partial renders with the record available, the same way a display_type: :page custom action partial does.

Hide a tab conditionally​

tab :constant_maps, "constant_maps", display_if: ->(record) { record.is_active? } do
column :name
end