Adding a new static page to an existing Rails application can be accomplished in a few easy steps:
1. Add a Static Controller
Enter rails g controller Static in your command line, which will generate a static controller (you should see the file static_controller.rb in your controllers directory). Inside this controller, add a show method as follows:
class StaticController < ApplicationController def show render params[:page] end end
2. Create Static Views
In your views directory, create a subdirectory called static. This directory will contain any static views you wish to generate. For example, if you wanted to add an about page to your Rails application, create a file called about.html.erb in your views/static directory.
3. Create Routes
Toward the end of your routes.rb file, add get “/:page” => “static#show”. This will account for any view templates included in your views/static directory. To link to this route in your application, enter <%= link_to “About”, “/about” %>, where the name of the route (/about) corresponds to the name of a template created in your views directory.
As you can see, this will create an about page at the route /about.
- PM Career Story - April 28, 2022
- How to Transition into Product Management - December 26, 2017
- What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015
Angela Inniss says
thanks this helped even in 2019! I’m learning Ruby 🙂