Hello again! Now that you're familiar with the basic structure of a Rails application, it's time to dive deeper into one of the core components: Controllers. In this lesson, we'll focus on what controllers are, how they fit into the Rails framework, and why they're essential for building robust web applications.
In this section, you'll learn how to create a controller, define actions, and connect them to views. A controller is the part of your Rails application that handles incoming HTTP requests and returns responses to the client. It acts as a middleman between the Model (where your data lives) and the View (what the user sees). Think of controllers as the conductors of your application, orchestrating the interactions between different parts of your app.
An HTTP request is a message sent by the client to a server, asking for some action to be performed. The most common types of HTTP requests are GET, POST, PUT, PATCH, and DELETE. GET requests are used to retrieve data from the server. When you type a URL into your browser and hit enter, you're making a GET request to that URL. POST requests are used to submit data to the server, PUT and PATCH requests are used to update existing data, and DELETE requests are used to remove data from the server.
In the context of the app that we considered earlier, we'll create a TodosController
that has two actions: index
and show
. We already know that these actions are used for displaying a list of resources/a single resource. Now it's time to implement those functions and we do it in app/controllers/todos_controller.rb
. Here’s a sneak peek at what it looks like:
Ruby1class TodosController < ApplicationController 2 def index 3 @todos = ['Todo 1', 'Todo 2', 'Todo 3'] 4 end 5 6 def show 7 @todo = "Showing todo with ID #{params[:id]}" 8 end 9end
This controller fetches a list of to-dos for the index
action and shows a specific to-do based on its ID for the show
action.
Ruby1class TodosController < ApplicationController
Defines a new class TodosController
that inherits from ApplicationController
, which is the base controller class in Rails.
Ruby1 def index 2 @todos = ['Todo 1', 'Todo 2', 'Todo 3'] 3 end
The index
action sets an instance variable @todos
to an array of to-do items. This action handles a GET
request to the /todos
URL and prepares the list of to-dos for the client.
Ruby1 def show 2 @todo = "Showing todo with ID #{params[:id]}" 3 end
The show
action sets an instance variable @todo
to a string that includes the id
parameter from the URL. This action handles a GET
request to /todos/:id
(e.g., /todos/1
) and prepares the specific to-do item for the client.
Considering we enable these actions in routes.rb
with resources :todos, only: [:index, :show]
, now when a request hits a specific route, Rails maps that request to the corresponding controller and action. For example:
- A
GET
request to/todos
will invoke theindex
action in theTodosController
. - A
GET
request to/todos/:id
will invoke theshow
action.
Remember how we created the project using a single command rails new
? Here we also have a shortcut that reduces manual work. These commands are called generators.
To generate a new controller, Rails provides a generator that sets up the basic structure for you. You can use the following command in your terminal:
Bash1rails generate controller Todos index show
This command will create several files, including the controller located at app/controllers/todos_controller.rb
.
Understanding controllers is crucial because they are the backbone of your application's flow. They handle user inputs, process data through models or services, and render appropriate views. By mastering controllers, you can better understand how requests are managed, processed, and responded to in a Rails application. This knowledge will enable you to build dynamic, data-driven web apps that interact seamlessly with users.
Excited to get hands-on with controllers? Let's start the practice section and build our TodosController
together!