Create Custom Controller Actions in Rails
Learn how to run the Rails controller generator to create static pages for the application.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that you know to utilize built-in RESTful routes, I thought it will be good to learn about some custom controller actions as well.

To start with, I'm going to create a controller and along with it some views that you can use, you can create the controller by running this generator.

rails g controller Pages contact about home

In this command, I'm creating a controller and calling it Pages, and I want a contact, about and home page for it. Though you can also create each page manually, it makes no sense as this is faster and less error prone.

This is what happens with that command:

large

Next, I start the Rails server with rails s If I go to the browser now and type localhost:3000/pages/about, you can see this page.

large

Likewise, I can go to contact and home and the same will be displayed. Now, if you go to the code, you can see these new pages. Go to app/views/pages in the file system and you can see there are three new files there called about.html.erb, contact.html.erb, and home.html.erb. You can go to each page and edit it using html tags, so it looks like just how you want. You can also see how your changes look in the browser.

In the next lesson, we'll start implementing custom routes for our application.