April 2019
Beginner to intermediate
244 pages
6h 34m
English
Our form is working well, but something is missing. If the user submits invalid data, the call to Repo.insert will fail, but we currently have no way of showing the user how to fix the problem. The changeset contains the validation errors, so we just need to add some elements to the page to display them. Let’s do that now.
We’ll start by adding a new helper function error_tag/2. This function is not provided by default so we need to define it ourselves. You can add it to any module you’re using for storing helper functions.
| | def error_tag(form, field) do |
| | if error = form.errors[field] do |
| | content_tag(:span, translate_error(error)) |
| | end |
| | end |
Read now
Unlock full access