Challenge - 5 Problems
Rails Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Rails display validation errors in a form?
Given a Rails form with a model that has validation errors, how does the
form_with helper display these errors by default?Attempts:
2 left
💡 Hint
Think about what Rails helpers do automatically when validations fail.
✗ Incorrect
Rails
form_with automatically generates a div with id='error_explanation' that lists all validation errors above the form fields, making it easy for users to see what went wrong.📝 Syntax
intermediate2:00remaining
Correct syntax to display error messages for a specific attribute
Which of the following is the correct way to display error messages for the
:email attribute of a model @user in a Rails view?Attempts:
2 left
💡 Hint
Look for the method that returns full error messages for a specific attribute.
✗ Incorrect
The method
full_messages_for(:attribute) returns an array of full error messages for that attribute. The other options either use incorrect method names or call methods on wrong objects.🔧 Debug
advanced2:00remaining
Why are error messages not showing in the form?
A developer notices that after submitting a form with invalid data, the error messages are not displayed, even though validations fail. The form uses
form_with model: @user. What is the most likely cause?Attempts:
2 left
💡 Hint
Think about what happens after a failed save in the controller.
✗ Incorrect
If the controller redirects after a failed save, the error messages stored in the model instance are lost. To display errors, the controller should re-render the form view with the invalid model instance.
❓ state_output
advanced2:00remaining
What is the content of
flash[:error] after a failed save?In a Rails controller, after
if @user.save fails, the developer sets flash[:error] = @user.errors.full_messages.join(", "). What will flash[:error] contain?Attempts:
2 left
💡 Hint
Consider what
join does to an array.✗ Incorrect
The
full_messages method returns an array of strings. Using join(", ") converts this array into a single string with messages separated by commas.🧠 Conceptual
expert3:00remaining
Why use
errors.add in custom validation methods?In Rails, when writing a custom validation method inside a model, why do we use
errors.add(:attribute, "message") instead of just raising an exception?Attempts:
2 left
💡 Hint
Think about user experience and how validations should behave.
✗ Incorrect
Using
errors.add allows Rails to collect all validation errors and display them to the user without stopping the program. Raising exceptions would interrupt the normal flow and is not suitable for validations.