Skip to main content

Has One Tab

A tab with relationship_type: :has_one shows a single associated record as a profile view, with Edit, Delete and custom actions, instead of a paginated list. When no record exists yet, the tab shows an empty state with an Add button. When the user lacks permission, it shows a permission denied state.

Quick example​

cm_show do
tab :profile, "profile",
associated_model: "profile",
layout_type: "cm_association_show",
relationship_type: :has_one,
show_count: false
end

Options​

tab(tab_name, action_name,
associated_model: nil,
layout_type: nil,
relationship_type: :has_many,
show_count: true,
display_if: nil,
allow_create_action: -> { true },
eager_load_associations: nil,
&block)

Pass relationship_type: :has_one alongside layout_type: 'cm_association_show' in the tab declaration. See tab for every option.

NameTypeRequiredDefaultDescription
tab_nameString | SymbolYesβ€”Tab label shown in the navigation.
action_nameStringYesβ€”Unique action name (used in routes and policy scopes).
associated_modelStringYesβ€”The has_one association name on the parent model.
layout_typeStringYesβ€”Must be 'cm_association_show' for has-one tabs.
relationship_typeSymbolYes:has_manySet to :has_one to enable single-record display.
show_countBooleanNotrueSet to false to hide the badge, or leave true to show 0/1.
display_ifProcNo-> { true }Lambda returning a boolean to conditionally show the tab.
allow_create_actionProcNo-> { true }Lambda controlling whether the Add button appears on the empty state.
eager_load_associationsArrayNonilAssociations to eager-load when fetching the record.

How it works​

relationship_type: :has_one changes what the tab renders:

StateWhat renders
Record exists and user has accessProfile view with fields, Edit/Delete/custom actions
Record does not existEmpty state with an Add button that pre-fills the association
User lacks permission (Pundit scope)Permission denied message

The tab badge shows 0 or 1 instead of a record count.

Behaviour details​

Empty state​

When no associated record exists, the tab renders an Add button. Clicking it opens the associated model's create form and pre-fills:

  • associated_id β€” the parent record's ID
  • associated_class β€” the parent model name
  • foreign_key β€” the foreign key column
  • polymorphic_name β€” set automatically for polymorphic associations

Permission denied state​

If a Pundit policy scope exists for the action and the record does not pass the scope check, the permission denied state is shown instead of the record. No record data is leaked.

Actions bar​

The profile view renders an actions bar above the record fields with:

  • Edit button β€” links to the associated model's edit page with a referrer param so the user returns to the parent show page after saving.
  • Custom actions dropdown β€” respects display_type (:button, :modal, :form_modal, :link).
  • Delete button β€” opens the destroy modal; shown only when the user has permission.

has_many vs has_one tab​

Behaviourhas_many (default)has_one
layout_typecm_association_indexcm_association_show
relationship_type:has_many (default):has_one
RendersPaginated table / card listSingle record profile view
No record stateEmpty tableEmpty state with Add button
Permission denied stateStandard Pundit scope on listDedicated permission denied view
Tab badgeRecord count0 or 1
show_countUsually trueUsually false

Recipes​

Full setup​

1. Model Setup​

class User < ApplicationRecord
has_one :profile
end

class Profile < ApplicationRecord
belongs_to :user
end

2. cm_admin Configuration​

# app/models/cm_user.rb
cm_admin do
cm_show do
tab :profile, 'profile',
associated_model: 'profile',
layout_type: 'cm_association_show',
relationship_type: :has_one,
show_count: false
end
end

# app/models/cm_profile.rb
cm_admin do
cm_show do
cm_section 'Personal Details' do
field :first_name
field :last_name
field :bio
end
end
end

3. Permissions​

Add the action to the Pundit policy and policy scope for the associated model. For example:

# app/policies/cm_admin/profile_policy.rb
class CmAdmin::ProfilePolicy < CmAdmin::ApplicationPolicy
class ProfileScope < Scope
def resolve
scope.where(user: user)
end
end

def show? = true
def edit? = true
def destroy? = true
end

After adding the tab, update the permissions for the relevant roles in the Permissions tab inside cm_admin.