Bird
Raised Fist0
Ruby on Railsframework~10 mins

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

Choose your learning style10 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.

    Practice

    (1/5)
    1. In Rails, which method provides a list of user-friendly error messages for a model's validation errors?
    easy
    A. @model.errors.count
    B. @model.errors.details
    C. @model.errors.keys
    D. @model.errors.full_messages

    Solution

    1. Step 1: Understand error message methods

      @model.errors.full_messages returns an array of readable error strings, while others return metadata or counts.
    2. Step 2: Identify user-friendly output

      Only full_messages gives clear, complete error messages suitable for display.
    3. Final Answer:

      @model.errors.full_messages -> Option D
    4. Quick Check:

      Error messages = full_messages [OK]
    Hint: Use full_messages for readable error text [OK]
    Common Mistakes:
    • Using errors.details which gives error codes, not messages
    • Using errors.keys which lists fields, not messages
    • Using errors.count which returns number, not messages
    2. Which of the following is the correct way to display error messages for a @user model in a Rails view?
    easy
    A. <%= @user.errors.full_messages.each do |msg| %>

    <%= msg %>

    <% end %>
    B. <% @user.errors.full_messages.each do |msg| %>

    <%= msg %>

    <% end %>
    C. <%= @user.errors.full_messages.map { |msg| content_tag(:p, msg) } %>
    D. <% @user.errors.full_messages.map { |msg| content_tag(:p, msg) } %>

    Solution

    1. Step 1: Understand ERB tags for iteration

      <% %> runs Ruby code without output; <%= %> outputs the result. For loops, use <% %>.
    2. Step 2: Identify correct syntax for displaying messages

      <% @user.errors.full_messages.each do |msg| %>

      <%= msg %>

      <% end %>
      correctly uses <% %> to loop and <%= %> inside to output each message.
    3. Final Answer:

      Option B -> Option B
    4. Quick Check:

      Loop with <% %>, output with <%= %> [OK]
    Hint: Use <% %> for loops, <%= %> for output inside loops [OK]
    Common Mistakes:
    • Using <%= %> for loops causing unwanted output
    • Using map without outputting results
    • Mixing output and code tags incorrectly
    3. Given this Rails code in a view:
    <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>

    What will be the output if @post.errors.full_messages contains ["Title can't be blank", "Content is too short"]?
    medium
    A.
    • Title can't be blank
    • Content is too short
    B.
      Title can't be blankContent is too short
    C.
  • Title can't be blank
  • Content is too short
  • D. No output because of syntax error

    Solution

    1. Step 1: Understand the loop output

      The code loops over each error message and wraps it in
    2. tags inside a <ul>.
    3. Step 2: Match output to HTML structure

      The output will be a <ul> list with two
    4. items showing each error message.
    5. Final Answer:

      <ul><li>Title can't be blank</li><li>Content is too short</li></ul> -> Option A
    6. Quick Check:

      Loop creates list items inside ul [OK]
    Hint: Errors inside <li> tags within <ul> show as list [OK]
    Common Mistakes:
    • Forgetting <ul> tags causing invalid HTML
    • Not using <li> tags for each message
    • Confusing output with plain text
    4. You wrote this code to display errors:
    <%= @article.errors.full_messages.each do |msg| %>
      <p><%= msg %></p>
    <% end %>

    But no errors show up in the browser. What is the problem?
    medium
    A. The <code><p>s
    B. You must call to_s on each message before displaying
    C. Using <code><%= %>s
    D. The errors array is empty, so nothing shows

    Solution

    1. Step 1: Check ERB tag usage for loops

      Using <%= %>each, which is the original array, not the loop content.
    2. Step 2: Correct loop tag usage

      Use <% %> for loops to run code without output, then use <%= %> inside for each message.
    3. Final Answer:

      Using <%= %> for the loop causes output of the array, not the messages -> Option C
    4. Quick Check:

      Use <% %> for loops, not <%= %> [OK]
    Hint: Use <% %> for loops, <%= %> for output inside loops [OK]
    Common Mistakes:
    • Using <%= %> for loops causing wrong output
    • Assuming errors are empty without checking
    • Misunderstanding HTML tag usage inside loops
    5. You want to display error messages only if there are any errors on the @comment model. Which is the best way to do this in a Rails view?
    hard
    A. <% if @comment.errors.any? %> <div role='alert' aria-live='assertive'> <ul> <% @comment.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
    B. <div> <ul> <% @comment.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div>
    C. <% if @comment.errors.count > 0 %> <p>Errors found</p> <% end %>
    D. <% unless @comment.errors.empty? %> <p>Please fix errors</p> <% end %>

    Solution

    1. Step 1: Check for errors presence

      Use errors.any? to check if there are any errors to display.
    2. Step 2: Display errors accessibly

      Wrap errors in a div with role='alert' and aria-live='assertive' for screen readers to announce changes.
    3. Step 3: Loop and show messages

      Use full_messages inside a ul with li for each error message.
    4. Final Answer:

      Option A -> Option A
    5. Quick Check:

      Check errors.any? and use accessible alert div [OK]
    Hint: Use errors.any? and role='alert' div for accessible errors [OK]
    Common Mistakes:
    • Not checking if errors exist before displaying
    • Missing accessibility roles for error containers
    • Displaying generic messages without details
    Recommended

    Ready to prove it?

    Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard