Controllers and Actions
Handling requests.
Controllers and Actions is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Controller?
A controller receives a request from the router, does the work, and decides what to send back. It is the coordinator between models and views.
- Controllers live in
app/controllers. - They inherit from
ApplicationController. - Each public method is an action.
Defining a Controller
A controller is a class ending in Controller. By convention ArticlesController handles routes for the articles resource.
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
endActions Map to Routes
Each of the seven REST routes calls a matching action. The router sends GET /articles to index, GET /articles/1 to show, and so on. Naming actions to match the convention keeps everything wired automatically.
The params Hash
Request data arrives in params, a hash-like object holding route segments, query strings, and form fields. For /articles/5, params[:id] is "5".
def show
@article = Article.find(params[:id])
endInstance Variables to Views
Controllers pass data to views through instance variables (the @ prefix). Any @variable set in the action is visible in the matching view template.
def index
@articles = Article.all
# @articles is available in index.html.erb
endImplementing index and show
The read actions are simple: index loads a collection, show loads one record by id. They then render their default templates.
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
endImplementing create
The create action builds a record from submitted data and saves it. On success it redirects; on failure it re-renders the form so the user can fix errors.
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render :new, status: :unprocessable_entity
end
endredirect_to vs render
Two very different responses:
redirect_totells the browser to make a new request to another URL.renderreturns a view directly for the current request.
Redirect after a successful write; render to show a form again with errors.
Before Actions
A before_action runs shared setup before chosen actions, reducing duplication. A common use is loading the record needed by show, edit, update, and destroy.
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
private
def set_article
@article = Article.find(params[:id])
end
endFilters and Authentication
before_action also enforces rules. A guard like before_action :require_login can redirect_to login_path and halt the action if the user is not signed in, protecting whole controllers at once.
before_action :require_login
private
def require_login
redirect_to login_path unless current_user
endHandling Missing Records
When find cannot locate a record it raises RecordNotFound, which Rails turns into a 404 in production. You can customize this with rescue_from to show a friendly page.
rescue_from ActiveRecord::RecordNotFound do
redirect_to articles_path, alert: "Not found"
endQuick Check
Test your understanding of controllers.
Recap: Controllers and Actions
You learned how controllers handle requests:
- Actions map to routes by convention.
paramscarries request data;@variablesreach the view.createsaves then redirects or re-renders.redirect_tovsrenderserve different purposes.before_actionshares setup and enforces auth.
Next we keep mass assignment safe with strong parameters.
Frequently asked questions
Is the “Controllers and Actions” lesson free?
Yes — the full text of “Controllers and Actions” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Controllers and Actions”?
Handling requests. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Controllers and Actions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Routes and Resources
- Controllers and Actions
- Strong Parameters
- Views and Rendering