I know UJS is on the way out and will be removed when TL 6 comes out, but how should it be done right now. With a fresh Rails 6 installation, whenever a UJS-enabled from submission fails to validate in the controller, nothing happens in the view. Flash messages or field errors don’t appear.
Do I really have to create a .js
response and handle it manually from there? Or am I missing something crucial?
To work around this, I took the Turbolinks approach by replacing the body if one is given:
function live(selector, event, callback, context) {
(context || document).addEventListener(event, function(e) {
var found, el = e.target || e.srcElement
while (el && el.matches && el !== context && !(found = el.matches(selector)))
el = el.parentElement
if (found) callback.call(el, e)
})
}
function handleUjsForm(event) {
if (event.detail[0].nodeType == 9) {
document.body.innerHTML = event.detail[0].body.innerHTML
} else if (event.detail[2].status >= 400) {
location.reload(true)
}
}
live('form[data-remote=true]', 'ajax:success', handleUjsForm)
live('form[data-remote=true]', 'ajax:error', handleUjsForm)
A bit hackish, but it works and plays nice with Turbolinks and Stimulus. Still, I’m wondering if there is a better way.