turboboost
turboboost copied to clipboard
Advanced error handling?
The current errors when using format.js { render_turboboost_errors_for(@post) }
is just a string array of error messages.
I'd like a fine grained error handling, but I don't know which field (attribute) the error corresponds to. Would it be possible to get an object like:
[{ name: 'Name can't be blank' }, { email: 'Email can't be blank' }]
(While retaining the same error output with #error_explanation
.)
The reason is that I'd like to style each form field that has an error.
Ideas?
Current workaround:
-
Don't use
Turboboost.insertErrors = true
-
Overrider the error messages JSON in an initializer.
module Turboboost
module Controller
extend ActiveSupport::Concern
def render_turboboost_errors_for(record)
render json: record.errors, status: :unprocessable_entity, root: false
end
end
end
- Use error handling callback
$(document).on('turboboost:error', function(e, errors) {
//errors is a string in this workaround
var jsonErrors = JSON.parse(errors),
$form = $(e.currentTarget);
//do whatever
});
Would be awesome if your gem would handle this :) I don't have time for a PR at the moment :(
Hi @Frexuz , if you prefer you can use the gem I just launched (turbolinks-form) to replace the whole page body after a submit.
This way you can show your errors anyway you want by re-rendering the form, and you will still have the performance of Turbolinks.
@hsgubert Looks cool :)