Skip to content
On this page

Select

The Select field renders a select field.

ruby
field :type, as: :select, options: { 'Large container': :large, 'Medium container': :medium, 'Tiny container': :tiny }, display_with_value: true, placeholder: 'Choose the type of the container.'

Options

options

A Hash representing the options that should be displayed in the select. The keys represent the labels, and the values represent the value stored in the database.

The options get cast as ActiveSupport::HashWithIndifferentAccess objects if they are a Hash.

Default

nil

Possible values

{ 'Large container': :large, 'Medium container': :medium, 'Tiny container': :tiny } or any other Hash.

enum

Set the select options as an Active Record enum. You may use options or enum, not both.

ruby
# app/models/project.rb
class Project < ApplicationRecord
  enum type: { 'Large container': 'large', 'Medium container': 'medium', 'Tiny container': 'small' }
end

# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
  field :type,
    as: :select,
    enum: ::Project.types,
    display_with_value: true,
    placeholder: 'Choose the type of the container.'
end

Default

nil

Possible values

Post::statuses or any other enum stored on a model.

display_value

You may want to display the values from the database and not the labels of the options. You may change that by setting display_value to true.

ruby
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
  field :type,
    as: :select,
    display_with_value: true
end

Default

false

Possible values

true, false

include_blank

Include blank

The Select field also has the include_blank option. That can have three values.

If it's set to false (default), it will not show any blank option but only the options you configured.

If it's set to true and you have a placeholder value assigned, it will use that placeholder string as the first option.

If it's a string include_blank: "No country", the No country string will appear as the first option in the <select> and will set the value empty or nil depending on your settings.

ruby
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
  field :type,
    as: :select,
    include_blank: 'No type'
end

Default

nil

Possible values

nil, true, false, or a string to be used as the first option.

Computed options

You may want to compute the values on the fly for your Select field. You can use a lambda for that where you have access to the model, resource, view, and field properties where you can pull data off.

ruby
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
  field :type,
    as: :select,
    options: ->(model: model, resource: resource, view: view, field: field) do
      model.get_types_from_the_database.map { |type| [type.name, type.id] }
    end,
    placeholder: 'Choose the type of the container.'
end

The output value must be a supported options_for_select value.