Skip to content

Radio

The Radio field is used to render radio buttons. It's useful when only one value can be selected in a given options group.

Field declaration example

Below is an example of declaring a radio field for a role:

ruby
field :role,
  as: :radio,
  name: "User role",
  options: {
    admin: "Administrator",
    manager: "Manager",
    writer: "Writer"
  }

Options

options

The options attribute accepts either a Hash or a proc, allowing the incorporation of custom logic. Within this block, you gain access to all attributes of Avo::ExecutionContext along with the record, resource, view and field.

This attribute represents the options that should be displayed in the radio buttons.

Default value

Empty Hash.

ruby
{}

Possible values

Any Hash. The keys represent the value that will be persisted and the values are the visible labels. Example:

ruby
options: {
  admin: "Administrator",
  manager: "Manager",
  writer: "Writer"
}

Or a Proc:

ruby
options: -> do
  record.roles.each_with_object({}) do |role, hash|
    hash[role.id] = role.name.humanize
  end
end

display_as

Controls how the radio buttons are rendered. When set to :tabs, the options are displayed as a tab-style switcher instead of standard radio buttons.

Default value

:radio

Possible values

:radio, :tabs

ruby
field :role, as: :radio, display_as: :tabs, options: { admin: "Administrator", writer: "Writer" }

display_value

When set to true, the Show view displays the stored value itself instead of looking up the matching label from the options hash.

Default value

false

Possible values

true, false

ruby
field :role, as: :radio, display_value: true, options: { admin: "Administrator", writer: "Writer" }