Get familiar with Rails actions and controllers in this lesson
We'll cover the following
We can’t help it—we just have to write a Hello, World! program to try a new system. Let’s start by creating a simple application that sends our cheery greeting to a browser. After we get that working, we’ll embellish it with the current time and links.
As you’ll explore further, in the The Architecture of Rails Applications chapter, Rails is a model-view-controller (MVC) framework. Rails accept incoming requests from a browser, decodes the request to find a controller, and calls an action method in that controller. The controller then invokes a particular view to display the results to the user. The good news is that Rails takes care of most of the internal plumbing that links all these actions. To write our Hello, World! application, we need code for a controller and a view, and we need a route to connect the two. We don’t need code for a model, because we’re not dealing with any data. Let’s start with the controller.
In the same way that we used the rails command to create a new Rails application, we can also use a generator script to create a new controller for our project. This command is rails generate
. So, to create a controller called say
, we make sure we’re in the demo
directory and run the command, passing in the name of the controller we want to create and the names of the actions we intend for this controller to support:
demo> bin/rails generate controller Say hello goodbye
A live terminal
Note: You can run the above command and see the output by using the below terminal.
The rails generate command logs the files and directories it examines, noting when it adds new Ruby scripts or directories to our application. For now, we’re interested in one of these scripts and (in a minute) the .html.erb
files.
The first source file we’ll be looking at is the controller. You can find it in the app/controllers/say_controller.rb
file. Let’s take a look at it:
class SayController < ApplicationControllerdef helloenddef goodbyeendend
Pretty minimal, eh? SayController
is a class that inherits from ApplicationController
, so it automatically gets all the default controller behavior. What does this code have to do? For now, it does nothing—we simply have empty action methods named hello()
and goodbye()
. To understand why these methods are named this way, you need to look at the way Rails handles requests.
Rails and requests URLs
Like any other web application, a Rails application appears to its users to be associated with a URL. When you point your browser at that URL, you’re talking to the application code, which generates a response to you.
Let’s try it now. Click Run
button to the below application. You’ll see something that looks like the following screenshot.
If you are on localhost then Navigate to the URL http://localhost:3000/say/hello
in a browser. However, in the widget, it will be automatically redirected to said link
A running application with a custom controller
We have provided the Say
controller with its two methods hello
and goodbye
in the below application.
Just run the below application to see things in action