0
0
Ruby on Railsframework~20 mins

View helpers in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
View Helper Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Rails view helper output?
Given the helper method below, what will be the rendered HTML output when called in a view?

module ApplicationHelper
  def greeting(name)
    content_tag(:p, "Hello, #{name}!")
  end
end

# Called in view as: <%= greeting('Alice') %>
Ruby on Rails
module ApplicationHelper
  def greeting(name)
    content_tag(:p, "Hello, #{name}!")
  end
end
A<p>greeting('Alice')</p>
BHello, Alice!
C<div>Hello, Alice!</div>
D<p>Hello, Alice!</p>
Attempts:
2 left
💡 Hint
Remember that content_tag creates an HTML tag with content inside.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Rails helper method to format a date?
You want to create a helper method that formats a Date object as 'Month day, Year' (e.g., 'April 5, 2024'). Which of the following method definitions is correct?
A
def format_date(date)
  date.strftime('%B %d, %Y')
end
B
def format_date(date)
  date.to_s('%B %d, %Y')
end
C
def format_date(date)
  date.format('%B %d, %Y')
end
D
def format_date(date)
  date.strftime('%b %d %Y')
end
Attempts:
2 left
💡 Hint
Check the Ruby Date class methods for formatting dates.
🔧 Debug
advanced
2:00remaining
Why does this helper method cause an error?
Consider this helper method:

def link_to_profile(user)
  link_to "Profile", user_path(user.id)
end

When called with link_to_profile(nil), it raises an error. Why?
Ruby on Rails
def link_to_profile(user)
  link_to "Profile", user_path(user.id)
end
ABecause user_path cannot accept an integer argument
BBecause user is nil, calling user.id raises NoMethodError
CBecause link_to requires a block when user is nil
DBecause the method link_to_profile is missing a return statement
Attempts:
2 left
💡 Hint
What happens if you call a method on nil in Ruby?
state_output
advanced
2:00remaining
What is the output of this helper with conditional logic?
Given this helper method:

def status_badge(active)
  if active
    content_tag(:span, "Active", class: "badge badge-success")
  else
    content_tag(:span, "Inactive", class: "badge badge-danger")
  end
end

# Called in view as: <%= status_badge(false) %>
Ruby on Rails
def status_badge(active)
  if active
    content_tag(:span, "Active", class: "badge badge-success")
  else
    content_tag(:span, "Inactive", class: "badge badge-danger")
  end
end
A<span class="badge badge-danger">Inactive</span>
B<span class="badge badge-success">Active</span>
C<span>Inactive</span>
DActive
Attempts:
2 left
💡 Hint
Check the condition and which content_tag is returned.
🧠 Conceptual
expert
2:00remaining
Which statement about Rails view helpers is true?
Choose the correct statement about Rails view helpers:
AHelpers can only be used in views and never in controllers or models
BHelpers are automatically available in views but must be explicitly included in controllers
CHelpers can be used to keep view templates clean by moving complex logic out of them
DHelpers must always return raw HTML strings without escaping
Attempts:
2 left
💡 Hint
Think about the purpose of helpers in MVC architecture.