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)
| Name | Type | Default | Description |
|---|---|---|---|
tab_name | String | Symbol | required | Tab label. |
action_name | String | required | "" for the main tab; otherwise a unique action name used in routes and permissions. |
associated_model | String | Symbol | nil | Association on the record to list (e.g. "comments"). |
layout_type | String | nil | cm_association_index (list) or cm_association_show (single record). |
layout | String | nil | Layout of the tab. |
partial | String | nil | Render this partial as the tab content. |
display_if | Proc | nil | Lambda returning whether to show the tab. |
view_type | Symbol | nil | :table or :card for association lists. |
show_count | Boolean | true | Show the record count badge. |
associated_model_name | String | inferred | Class name of the associated model when it differs from the association. |
allow_create_action | Proc | -> { true } | Whether the Add button is shown in the tab. |
eager_load_associations | Array | nil | Associations to preload for the listed records. |
relationship_type | Symbol | :has_many | :has_many or :has_one. See Has One Tab. |
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