0
0
Ruby on Railsframework~10 mins

Error messages and display in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<%= pluralize(@user.errors.count, "error") %>
B<%= @user.errors.full_messages %>
C<%= @user.errors.count %>
D<%= @user.errors.messages %>
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.
2fill in blank
medium

Complete 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'
Amessage.full
Bmessage.to_s
C@user.errors.full_messages
Dmessage
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.
3fill in blank
hard

Fix 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'
A<%= pluralize(@post.errors.count, "error") %>
B<%= @post.errors.count %>
C<%= @post.errors.full_messages.count %>
D<%= @post.errors.messages.count %>
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.
4fill in blank
hard

Fill 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'
Amsg
Bmessages
Cmsg.html_safe
Dmodel.errors.full_messages
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.
  • 5fill in blank
    hard

    Fill 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'
    A@article
    B<%= pluralize(@article.errors.count, "error") %>
    Carticle
    D@comment
    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.