The controller action greet sets @message to "Hello, Rails!" which is accessible in the view.
Step 2: View renders the instance variable inside <h1> tags
The view uses ERB syntax <%= @message %> to insert the message text inside an <h1> element.
Final Answer:
<h1>Hello, Rails!</h1> -> Option C
Quick Check:
Instance variable rendered in view = B [OK]
Hint: Instance variables in controller appear in views with <%= %> [OK]
Common Mistakes:
Expecting raw variable name instead of its value
Confusing plain text with HTML tags
Assuming variable is undefined without @
4. Identify the error in this controller code snippet:
class ProductsController < ApplicationController
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def create
product = Product.new(params[:product])
if product.save
redirect_to product_path(product)
else
render :new
end
end
end
medium
A. Using params[:product] directly without strong parameters
B. Missing return statement in create action
C. No instance variable @product in create action
D. Incorrect inheritance from ApplicationController
Solution
Step 1: Review how parameters are handled in create action
The code uses params[:product] directly, which is unsafe and disallowed in modern Rails without strong parameters.
Step 2: Identify missing strong parameters usage
Rails requires filtering params with methods like product_params to permit only allowed attributes.
Final Answer:
Using params[:product] directly without strong parameters -> Option A
Quick Check:
Strong parameters required = D [OK]
Hint: Always use strong parameters to permit attributes [OK]
Common Mistakes:
Ignoring strong parameters and using raw params
Thinking return statements are needed in Rails actions
Confusing instance variables with local variables
5. You want a controller action to handle both showing a form and processing form data for creating a new user. Which approach best follows Rails controller patterns?
hard
A. Use one action create to show form and save user
B. Use one action new to both show form and save user
C. Handle form display and data processing in the view file
D. Use separate actions: new to show form, create to process data