Select
The Select field renders a select field.
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
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
# 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.'
endDefault
nil
Possible values
Post::statuses or any other enum stored on a model.
-> display_value
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
field :type,
as: :select,
display_with_value: true
endDefault
false
Possible values
true, false
-> 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.
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
field :type,
as: :select,
include_blank: 'No type'
endDefault
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.
# app/avo/resources/project_resource.rb
class ProjectResource < Avo::BaseResource
field :type,
as: :select,
options: ->(model:, resource:, view:, field:) do
model.get_types_from_the_database.map { |type| [type.name, type.id] }
end,
placeholder: 'Choose the type of the container.'
endThe output value must be a supported options_for_select value.
Friendly.rb - Your friendly European Ruby Conference