Validations with form_tag
Validations with form_tag
form_tagObjectives
Render or redirect based on validation of instance in create/update
Prefill in form values based on an instance
Print out full error messages based on an invalid instance
Introspect on errors for a field
Apply an error class to a field conditionally based on errors on a field
Requirements
Validations
Add validations to
Authorsuch that...nameis not blankemailis uniquephone_numberis exactly 10 digits longAdd validations to
Postsuch that...titleis not blankcontentis at least 100 characters longcategoryis either"Fiction"or"Non-Fiction"
Basic Routes & Controllers
Create controllers for both models.
Create
show,new,edit,create, andupdateroutes for both models.Define controller actions for
show,new, andedit.Define the "valid path" for the
createandupdatecontroller actions.Define the "invalid path" for the
createandupdatecontroller actions.
Forms
Create forms with
form_tagfor both models'newandeditactions.Prefill already-submitted forms with the invalid data when re-rendering.
Display a list of errors at the top of forms when an invalid action is attempted. They should be contained in an element with id
error_explanation, and each error should have its own<li>.Conditionally wrap each field in a
.field_with_errorsdiv if it has errors.
Clone : https://github.com/learn-co-curriculum/validations-with-form_tag-rails-lab
Last updated