Challenge - 5 Problems
View Helper Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
Attempts:
2 left
💡 Hint
Remember that content_tag creates an HTML tag with content inside.
✗ Incorrect
The content_tag helper wraps the string inside the specified HTML tag, here a paragraph
. So the output is a paragraph with the greeting text.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the Ruby Date class methods for formatting dates.
✗ Incorrect
The strftime method formats dates with the given pattern. '%B' is full month name, '%d' is day with leading zero, '%Y' is four-digit year.
🔧 Debug
advanced2:00remaining
Why does this helper method cause an error?
Consider this helper method:
When called with
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
Attempts:
2 left
💡 Hint
What happens if you call a method on nil in Ruby?
✗ Incorrect
Calling user.id when user is nil causes NoMethodError since nil has no id method.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
Check the condition and which content_tag is returned.
✗ Incorrect
When active is false, the else branch runs, producing a span with class 'badge badge-danger' and text 'Inactive'.
🧠 Conceptual
expert2:00remaining
Which statement about Rails view helpers is true?
Choose the correct statement about Rails view helpers:
Attempts:
2 left
💡 Hint
Think about the purpose of helpers in MVC architecture.
✗ Incorrect
Helpers are designed to keep views simple by moving formatting and presentation logic out of templates.