Skip to main content

Sections

cm_section is a titled block of fields on a show page or form. Besides field, form_field and nested_form_field, a section can hold rows and nested_form_sections β€” a section inside another section.

Quick example​

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

nested_form_section "Contact" do
field :email
field :phone
end
end
end
end

cm_section​

Options​

cm_section(section_name, display_if: nil, col_size: nil, html_attrs: nil, partial: nil, dynamic_fields: nil, &block)
NameTypeDefaultDescription
section_nameStringrequiredSection title ("" for none).
display_ifProcnilLambda receiving the record; return false to hide.
col_sizeIntegernilWidth in the 12-column grid.
html_attrsHashnilHTML attributes for the section element.
partialStringnilRender this partial as the section content.
dynamic_fieldsProcnilLambda evaluated per record that declares the section's fields. See below.
cm_section("Basic Information", display_if: ->(current_object) { current_object.name == "John" }, col_size: 6, html_attrs: { class: "section-class" }) do
field :title
end

cm_show_section is deprecated: it is an alias of cm_section without col_size. Use cm_section.

Recipes​

Render a partial as the section content​

cm_section "Preview", partial: "/cm_admin/blog_post/preview"

Dynamic fields​

dynamic_fields takes a lambda that runs per record and declares the section's fields by calling field, form_field and the other section methods. Use it when the field list depends on the record itself, instead of writing many display_ifs. The lambda receives (record, associated_record) β€” the second argument is the record selected in a nested context.

Fields that depend on the record. A settings page where the input type comes from the setting's own definition:

cm_edit page_title: "Edit Setting" do
cm_section "Setting Details", dynamic_fields: ->(record, _assoc) {
setting = CmPlatformSetting::DEFAULT_SETTINGS.find { |s| s[:name] == record.name }
input_type = setting&.dig(:input_type) || :text

form_field :description, input_type: :text
form_field :value, input_type: input_type, collection: setting&.dig(:collection)
}
end

Fields that depend on an associated record. A form where picking a job in a nested context loads that job's questions:

cm_new do
cm_section "Job", dynamic_fields: ->(job_application, job) {
job_application.job ||= job if job.is_a?(Job)
job_application.build_answers_for_questions

if job_application.job.present?
form_field :job_id, input_type: :hidden, is_required: true
else
form_field :job_id, input_type: :single_select, helper_method: :select_options_for_open_job,
helper_text: "Select a job to load its questions"
end
form_field :job_title, input_type: :string, disabled: ->(_) { true },
display_if: ->(obj) { obj.job.present? }
}
end

dynamic_fields also works on show pages β€” the lambda is evaluated per record before the section renders.

Hide a section conditionally​

cm_section "Status Change Log", display_if: ->(record) { record.status_changes.any? } do
field :approved_by_name
end

nested_form_section​

A section nested inside a cm_section. On forms it renders a titled sub-group inside the parent section instead of a separate card; on show pages it renders a nested card inside the parent section's card. It holds the same things a cm_section does β€” field, form_field, nested_form_field and row β€” and takes the same options except dynamic_fields.

Options​

nested_form_section(section_name, display_if: nil, col_size: nil, html_attrs: nil, partial: nil, &block)
NameTypeDefaultDescription
section_nameStringrequiredSub-heading shown inside the parent section.
display_ifProcnilLambda receiving the record; return false to hide.
col_sizeIntegernilWidth in the 12-column grid.
html_attrsHashnilHTML attributes for the nested section element.
partialStringnilRender this partial as the nested section's content.

Recipes​

Group fields inside a form section​

cm_new page_title: "Add User" do
cm_section "User Details" do
form_field :name, input_type: :string
form_field :status, input_type: :single_select, helper_method: :select_options_for_user_status

nested_form_section "Contact" do
form_field :email, input_type: :email
form_field :phone, input_type: :phone
end
end
end

The form fields under "Contact" render grouped under their own sub-heading, inside the "User Details" section.

Group fields on a show page​

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

nested_form_section "Contact" do
field :email
field :phone
end
end
end
end

Hold a nested form field​

cm_new page_title: "Add Job" do
cm_section "Job" do
form_field :title, input_type: :string

nested_form_section "Questions" do
nested_form_field :job_questions, submit_text: "+ Add Question" do
form_field :question, input_type: :string
form_field :is_required, input_type: :switch
end
end
end
end

nested_form_field needs accepts_nested_attributes_for on the model β€” see Nested form fields.