Add a new homepage

This guide is a part of the Rails Girls workshop main guides. Make sure you follow the numbered guides in order before continuing.

In this guide we’ll add another page. This will be our new homepage: the first page that will be shown when you open your app when you visit http://localhost:3000. Feel free to skip this guide if you know how Rails controllers, views and routes work.

In the previous guide a “pages” controller was already generated, we do not need to do this again. Rails will stop us if we do try to. Instead, we’ll need to add the page manually ourselves.

Add a new view

Instead we’ll add another page on our own. Run the following command in the Terminal app to add another “view” file used to display page content.

touch app/views/pages/homepage.html.erb
ni app/views/pages/homepage.html.erb

Then open the newly created file in your Text editor: app/views/pages/homepage.html.erb

Add some content to it, like the following, and save the file:

<div class="px-4 py-5 my-5 text-center">
  <h1 class="display-5 fw-bold">The ideas app</h1>
  <div class="col-lg-6 mx-auto">
    <p class="lead mb-4">Welcome to my ideas app!</p>

Configuring the route

To tell Rails when to show this page, open the config/routes.rb file in your Text Editor. Change the following line:

root to: redirect("/ideas")

to this instead and save the file:

root "pages#homepage"

When you now visit the root path of the app, http://localhost:3000, you should see your new homepage!

Updating the navigation bar

Lastly, to make the new root page accessible through the navigation bar, open the app/views/layouts/application.html.erb file in your Text Editor. Above the following lines:

<li class="nav-item">
  <a class="nav-link <%= 'active' if current_page?(controller: 'ideas') %>" href="/ideas">Ideas</a>

add a new link with the lines below, and save the file:

<li class="nav-item">
  <a class="nav-link <%= 'active' if current_page?(controller: 'pages', action: 'homepage') %>" href="/">Home</a>

When you refresh the page in the browser, and click the “The ideas app” link, it will open the new homepage. Try out all the links in the navigation bar. Do they take you to the page you expected?

If you’re ever stuck during a guide, please ask your coach for help and also consult this handy cheatsheet for Ruby, Rails, the console, the Text Editor etc.


View all guides