Form

Building Rails forms.

Anatomy

Form do |f|
# Basic input
f.input :name
# Input with custom label and hint
f.input :email do |input|
input.label "Email Address"
input.hint "We'll never share your email"
end
# Other form fields
f.textarea :description
f.checkbox :terms_accepted
f.switch :notifications_enabled
# Collection-based fields
f.checkbox_group :interests, collection, value_method: :id, text_method: :name
f.radio_group :size, sizes, value_method: :value, text_method: :label
f.select :country, countries, value_method: :code, text_method: :name
f.combobox :category, categories, value_method: :id, text_method: :title
# Date and range inputs
f.date_picker :birth_date
f.date_range_picker :start_date, :end_date
f.slider :rating, min: 1, max: 10
# Submit button
f.submit "Create Account"
end

API Reference

Form

Wrapper for form_with.

NameTypeDefault
model:objectfalse
loading:booleanfalse
Whether to render a LoadingButton.

Form#input

NameTypeDefault
methodsymbolnil
Used for the input's name. If model is passed in, it will be used for building the input's name, value, and error message.
type:symbol:text
Input type (text, email, password, etc.)
value:stringnil
If model is passed in, it will use Model#method as the value.
label:stringnil
Label for the input. If model is passed in, it will render a label using Rails' label helper.
hint:stringnil
Hint message to display below the input.
error:stringnil
Error message to display below the input. If model is passed in, it will use model.errors.full_messages_for(method) as the error message.
attributeskeyword arguments

Form#textarea

NameTypeDefault
methodsymbolnil
Used for the textarea's name and value binding.
value:stringnil
If model is passed in, it will use Model#method as the value.
label:stringnil
hint:stringnil
error:stringnil
attributeskeyword arguments

Form#checkbox

NameTypeDefault
methodsymbolnil
value:string"1"
Value to submit when checked.
checked:booleannil
Whether the checkbox is checked. Automatically determined from model if present.
label:stringnil
hint:stringnil
error:stringnil
attributeskeyword arguments

Form#switch

NameTypeDefault
methodsymbolnil
value:string"1"
checked:booleannil
Whether the switch is on. Automatically determined from model if present.
label:stringnil

Form#checkbox_group

NameTypeDefault
methodsymbolnil
collectionarray[]
Collection of items to display as checkboxes.
value_method:symbol
Method to call on each collection item for the checkbox value.
text_method:symbol
Method to call on each collection item for the checkbox label.
attributeskeyword arguments

Form#radio_group

NameTypeDefault
methodsymbolnil
collectionarray[]
Collection of items to display as radio buttons.
value_method:symbol
Method to call on each collection item for the radio value.
text_method:symbol
Method to call on each collection item for the radio label.
attributeskeyword arguments

Form#select

NameTypeDefault
methodsymbolnil
collectionarray[]
Collection of items to display as options.
value_method:symbol
Method to call on each collection item for the option value.
text_method:symbol
Method to call on each collection item for the option text.
disabled_items:arraynil
Array of values that should be disabled.
attributeskeyword arguments

Form#combobox

NameTypeDefault
methodsymbolnil
collectionarray[]
Collection of items to display as options.
value_method:symbol
Method to call on each collection item for the option value.
text_method:symbol
Method to call on each collection item for the option text.
attributeskeyword arguments

Form#date_picker

NameTypeDefault
methodsymbolnil
value:string/datenil
label:stringnil
hint:stringnil
error:stringnil
attributeskeyword arguments

Form#date_range_picker

NameTypeDefault
methodsymbolnil
Method for the start date.
end_methodsymbolnil
Method for the end date.
attributeskeyword arguments

Form#slider

NameTypeDefault
methodsymbolnil
Method for single value or range start.
end_methodsymbolnil
Method for range end value (for range sliders).
attributeskeyword arguments

Form#submit

Submit button with loading state support.

NameTypeDefault
valuestringnil
Button text. Defaults to Rails' localized submit values (Create/Update Model).
variant:symbol:default
Button variant (default, destructive, outline, secondary, ghost, link).
attributeskeyword arguments

Examples