ActionController: actions
This course covers Rails controller actions, using a real controller from this project. An action is an ordinary public method on a class that inherits from ApplicationController, matched by a route's controller#action string. The lessons explain the conventional names index, show, new and create, how instance variables carry data to the view, and how an action renders its template without a render call. They also introduce params and walk through a create action that saves a record, then redirects or re-renders the form with a 4xx status.
What you'll learn
- ✓What a controller action is and how a route's controller#action points to it
- ✓The meaning of the conventional action names index, show, new and create
- ✓How instance variables hand data from an action to its view
- ✓How implicit rendering picks the template named after the action
- ✓How params holds values from dynamic segments and submitted forms
- ✓How create saves, redirects on success and renders :new with unprocessable_content on failure
What is a controller action?
Rails routes: basics ended with get "partners" => "public/pages#partners", as: :partners, naming a destination without opening it. Here is that destination in this project:
Read the full lesson free →Curriculum
7 lessonsWhat is a controller action?
A controller action is a public method that coordinates other objects and runs once per request.
index, show, new, create: four conventional names
The names index, show, new and create carry conventional meanings for listing, displaying, blank forms and saving.
@variables: the bridge to the view
Instance variables set in an action stay alive after the method ends, so the view can read them.
Implicit rendering: no render call needed
An action without render or redirect_to renders the template named after it in the controller's folder.
params: a first look
The params object holds request values from dynamic URL segments and from form fields nested under the form's name.
create: save, then redirect
A create action saves a record, redirects on success, and renders :new with a 4xx status on failure.
Putting it together: a controller, fully read
The partners action is read again to show which lesson ideas it uses and which belong to create.