Goal
You’ll be able to write basic controller actions, and understand exactly what “params” represents and where its data actually comes from.
Learn
A controller groups related actions together, each one handling a specific kind of request:
class ProductsController < ApplicationController
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
end params is a hash-like object containing all the data submitted with the current request — URL segments (like :id from the route), query string parameters, and form data submitted via POST all end up accessible through params.
Instance variables (prefixed with @, like @product) set inside a controller action are automatically available inside the corresponding view — this is the actual mechanism connecting a controller’s data to what gets displayed, not magic, just Rails’ specific convention for passing data from controller to view.
By default, an action implicitly renders a view matching its own name — the show action above automatically renders app/views/products/show.html.erb, with no explicit render call needed, following the same naming-convention pattern covered in Lesson 1. You can override this default with an explicit render call if you genuinely need to render something different.
Decision Task
A controller action does @product = Product.find(params[:id]) but the corresponding view references @item instead of @product. Before reading on: what happens, and why?
Show Answer
The view would show nil (or raise an error trying to call a method on nil) for @item, since only @product was actually set in the controller — instance variables must match exactly between the controller action and the view for the data to actually be accessible; there’s no automatic renaming or connection beyond the literal variable name matching.
Common Mistake
Assuming a view can access any variable set anywhere in the controller, rather than understanding that specifically instance variables (@variable) are what get passed to the view. A plain local variable set in a controller action (without the @ prefix) is NOT accessible in the corresponding view — only instance variables cross that boundary, which is a genuinely important, specific mechanism, not a general “the view can see everything” assumption.
Practice Questions
1. Write a controller action called show that finds a Book by its id from params and assigns it to an instance variable.
Show Answer
def show\n @book = Book.find(params[:id])\nend
2. What does params contain?
Show Answer
All the data submitted with the current request — URL segments, query string parameters, and form data.
3. Why does a view need to reference the exact same instance variable name the controller set, not just “any” data from the controller?
Show Answer
Because instance variables are the specific mechanism connecting controller and view; only instance variables (prefixed with @) actually cross that boundary, and the name must match exactly for the data to be accessible.
4. What does a controller action render by default, with no explicit render call?
Show Answer
A view file matching the action’s own name, following Rails’ naming convention (e.g., the show action renders show.html.erb by default).
5. True or False: a plain local variable (no @ prefix) set inside a controller action is automatically accessible in the corresponding view.
Show Answer
False — only instance variables (with the @ prefix) are passed to the view; plain local variables stay local to the controller action.
Try It Yourself
Without looking back, write a controller action called index that assigns all Comment records to an instance variable.
Show Answer
def index\n @comments = Comment.all\nend
Quick Check
1. What does a controller group together?
Show Answer
Related actions, each handling a specific kind of request.
2. Why must a view reference the exact same instance variable name the controller set, not just any variable name?
Show Answer
Because instance variables are the specific mechanism connecting controller and view; the name must match exactly for the data to actually be accessible, since there is no automatic renaming or general access to controller data beyond this specific match.
3. What symbol prefixes a variable that gets passed from controller to view?
Show Answer
@ (instance variable).
4. What does an action render by default, with no explicit render call?
Show Answer
A view file matching the action’s own name.
5. Can a view access a plain local variable set in the controller action?
Show Answer
No — only instance variables cross that boundary.