Challenge - 5 Problems
Rails Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Rails controller action render?
Consider this Rails controller action:
What will this action render by default when called?
def show @user = User.find(params[:id]) end
What will this action render by default when called?
Attempts:
2 left
💡 Hint
Rails automatically renders a view matching the action name unless told otherwise.
✗ Incorrect
In Rails, if an action does not explicitly call render or redirect, it automatically renders the template matching the action name in the controller's view folder.
❓ state_output
intermediate2:00remaining
What is the value of @message after this action runs?
Given this Rails controller action:
If the URL is '/greet?name=Alice', what is the value of @message?
def greet @message = params[:name] || 'Guest' end
If the URL is '/greet?name=Alice', what is the value of @message?
Attempts:
2 left
💡 Hint
Check if params[:name] exists and what value it holds.
✗ Incorrect
Since the URL provides 'name=Alice', params[:name] is 'Alice', so @message is set to 'Alice'.
📝 Syntax
advanced2:00remaining
Which option correctly defines a Rails action that redirects to root path?
You want an action named 'home' that redirects users to the root path. Which code is correct?
Attempts:
2 left
💡 Hint
Look for the correct Rails method to redirect and the correct argument.
✗ Incorrect
'redirect_to' is the correct method to redirect, and 'root_path' is the helper for the root URL.
🔧 Debug
advanced2:00remaining
Why does this action raise an error?
Examine this Rails controller action:
Why might this code raise an error?
def update @post = Post.find(params[:id]) @post.update(params[:post]) redirect_to @post end
Why might this code raise an error?
Attempts:
2 left
💡 Hint
Check how Rails handles strong parameters for mass assignment.
✗ Incorrect
Rails requires strong parameters to permit attributes for update. Passing params[:post] directly causes a ForbiddenAttributesError.
🧠 Conceptual
expert2:00remaining
Which statement about Rails action methods is true?
Select the correct statement about action methods in Rails controllers.
Attempts:
2 left
💡 Hint
Think about how data flows from controller to view in Rails.
✗ Incorrect
Action methods have access to params and can set instance variables that views use to display data.