Hello,
At the end of page 140 the book implies that assoc_constraint
means that you should get a descriptive form validation error when submitting the video new/edit form with a category ID that doesn’t exist:
As with unique_constraint, when we set up assoc_constraint, we no longer get Ecto.ConstraintError. Instead, they’re converted into changeset error messages.
You can try to reproduce this constraint error via our web application in a couple of ways. For example, you could load the page, then remove the category from the database and submit the form after choosing the removed category. If you feel a bit more sneaky, you can fiddle the select options in the browser console, changing their value and then submitting the form.
However, the book never tells you to add an error_tag
to the category
select field, nor is it included in the source code that comes with the book, so there is in fact no way to just reproduce the error in the web application as per the book.
Intuitively I would also have expected to be able to use <%= error_tag f, :category_id %>
, since the actual select field is named like that:
<%= select f, :category_id, category_select_options(@categories), prompt: "Choose a category" %>
But since it’s an assoc_constraint
on :category
the actual error is on :category
rather than :category_id
, so you need to use <%= error_tag f, :category %>
instead, which is not explained in the book either, and it took me a while to figure out why it wasn’t being rendered.
It’s also not entirely clear to me whether just <%= error_tag f, :category %>
is enough, or whether I might need a second error_tag
with :category_id
as well to cover all potential validation errors?