Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to display error messages for a model in a Rails view.
Ruby on Rails
<% if @user.errors.any? %> <div id="error_explanation"> <h2>[1] errors prohibited this user from being saved:</h2> </div> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @user.errors.full_messages directly in the header instead of the count.
Forgetting to pluralize the word 'error'.
Using @user.errors.messages which is a hash, not a string.
✗ Incorrect
The pluralize helper formats the error count with correct singular/plural form, which is standard in Rails error displays.
2fill in blank
mediumComplete the code to list all error messages for a model in a Rails view.
Ruby on Rails
<ul>
<% @user.errors.full_messages.each do |message| %>
<li>[1]</li>
<% end %>
</ul> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call methods on 'message' which is already a string.
Using @user.errors.full_messages inside the loop instead of the loop variable.
✗ Incorrect
Inside the loop, 'message' is a string representing one error message, so just output 'message'.
3fill in blank
hardFix the error in the code to correctly display error messages for a model in a Rails view.
Ruby on Rails
<% if @post.errors.any? %> <div id="error_explanation"> <h2>[1] errors prevented this post from saving:</h2> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using count alone without pluralize, which shows just a number without the word.
Using full_messages.count which is the same as errors.count but missing pluralize formatting.
✗ Incorrect
The pluralize helper formats the error count with correct singular/plural form, which is the correct way to show error count in Rails.
4fill in blank
hardFill both blanks to create a Rails helper method that returns formatted error messages for a model.
Ruby on Rails
def formatted_errors(model) return "" if model.errors.empty? messages = model.errors.full_messages.map { |msg| "<li>[1]</li>" }.join "<ul>[2]</ul>".html_safe end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using model.errors.full_messages directly inside the map block instead of 'msg'.
Putting the entire messages string inside each instead of joining after mapping.
✗ Incorrect
Inside map, use 'msg' to insert each message. Then wrap all messages in
- tags using the 'messages' string.
5fill in blank
hardFill all three blanks to display error messages with a heading and list in a Rails view partial.
Ruby on Rails
<% if [1].errors.any? %> <div id="error_explanation"> <h2>[2] errors prohibited this [3] from being saved:</h2> <ul> <% [1].errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not pluralizing the error count in the heading.
Using instance variable syntax inside the heading string.
✗ Incorrect
Use '@article' as the model variable, pluralize the error count for the heading, and use 'article' as the model name in the message.