Quick Start
Let's start with the simplest scenario possible: you have a form with multiple input elements, and when the user clicks on the Submit button you want to display any validation error messages beside the elements that have issues. When the user resolves these issues and clicks the Submit button, the form is processed normal and the page navigates to whatever comes next.
Model
Validations are covered in-depth by the official documentation. Optimism doesn't require anything special.
Optimism is designed for ActiveRecord models that have validations defined, although it should work with any Ruby class that implements Active Model and has an errors
accessor.
View
Here's sample form partial for a Post model. It has two attributes - name and body and was generated with a Rails scaffold command.
And here is that same form partial, configured to work with Optimism:
Eagle-eyed readers will see that setting up a bare-bones Optimism integration requires removing two things and adding one thing to each attribute:
Remove
local: true
from theform_with
on the first lineRemove the error messages block from lines 2-12 entirely
Add an
error_for
helper for each attribute
The error_for
helper creates an empty span
tag with an id such as posts_body_error, and this is where the error messages for the body attribute will appear.
Even though form_with
is remote-by-default, many developers were confused and frustrated by the lack of opinionated validation handling out of the box for remote forms. Since scaffolds are for new users to get comfortable, remote forms are disabled. This is the primary reason that Optimism was created: we want our tasty remote forms without any heartburn.
Controller
The last step is to slightly modify the create and update actions in our PostsController. The other actions have been removed for brevity:
The only meaningful change required (as seen on lines 8 and 20 in this example) is to replace render :new
and render :edit
with a call to broadcast_errors
which has two mandatory parameters: the model instance and the list of attributes to validate. Usually this is the whitelisted params hash, but you can pass a subset as small as one attribute to be validated.
That's all there is to it. You now have live - if unstyled - form validations being delivered over websockets.
Last updated