Koren Leslie Cohen

  • About
  • Blog
  • Contact

1 comment

Ruby / Rails

Adding a Static Page to a Rails Application

July 20, 2014 by Koren Leslie Cohen

preloader

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.

staticny

As you can see, this will create an about page at the route /about.

  • About
  • Latest Posts
Connect
Koren Leslie Cohen
Product manager at Facebook. Former senior product manager at Dollar Shave Club in Los Angeles and software engineer at J.Crew / Madewell in New York City. Recovering trial lawyer.
Connect
Latest posts by Koren Leslie Cohen (see all)
  • 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

Related Posts

FizzBuzz in Ruby (and the Strangeness of Ruby Loops)
Rails Tutorial: Authentication with Devise

Share

Facebook Google+ Twitter Pinterest Email

Comments Cancel reply

Your email address will not be published. Required fields are marked *

*

code

  1. Angela Inniss says

    May 12, 2019 at 3:42 pm

    thanks this helped even in 2019! I’m learning Ruby 🙂

    Reply

Back to Blog

  • GitHub
  • Instagram
  • LinkedIn
  • RSS
  • Twitter

Looking for something?

Copyright 2023 Koren Leslie Cohen