0
0
Ruby on Railsframework~20 mins

Action methods in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Rails controller action render?
Consider this Rails controller action:
def show
  @user = User.find(params[:id])
end

What will this action render by default when called?
AIt renders the template 'show.html.erb' inside the 'users' folder automatically.
BIt renders plain text with the user's name.
CIt redirects to the index action of the controller.
DIt raises an error because no render or redirect is specified.
Attempts:
2 left
💡 Hint
Rails automatically renders a view matching the action name unless told otherwise.
state_output
intermediate
2:00remaining
What is the value of @message after this action runs?
Given this Rails controller action:
def greet
  @message = params[:name] || 'Guest'
end

If the URL is '/greet?name=Alice', what is the value of @message?
AAn error is raised because params[:name] is a string
B'Alice'
Cnil
D'Guest'
Attempts:
2 left
💡 Hint
Check if params[:name] exists and what value it holds.
📝 Syntax
advanced
2: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?
A
def home
  redirect root_path
end
B
def home
  render root_path
end
C
def home
  redirect_to root_path
end
D
def home
  redirect_to :root
end
Attempts:
2 left
💡 Hint
Look for the correct Rails method to redirect and the correct argument.
🔧 Debug
advanced
2:00remaining
Why does this action raise an error?
Examine this Rails controller action:
def update
  @post = Post.find(params[:id])
  @post.update(params[:post])
  redirect_to @post
end

Why might this code raise an error?
Aparams[:post] is not permitted, causing a ForbiddenAttributesError.
BPost.find(params[:id]) returns nil, causing a NoMethodError.
Credirect_to @post is invalid syntax and causes a syntax error.
Dupdate method does not exist on ActiveRecord objects.
Attempts:
2 left
💡 Hint
Check how Rails handles strong parameters for mass assignment.
🧠 Conceptual
expert
2:00remaining
Which statement about Rails action methods is true?
Select the correct statement about action methods in Rails controllers.
AAction methods must always explicitly call render or redirect_to to produce a response.
BPrivate methods in controllers can be used as actions if called via routes.
CAction methods run only once when the server starts, then cache their output.
DAction methods can access params and instance variables to pass data to views.
Attempts:
2 left
💡 Hint
Think about how data flows from controller to view in Rails.