Adding static pages to your Rails app
Sooner or later you want to pages to your Rails application that are not created dynamically, but rather contain either some marketing information, legal info like imprint or terms, or even feature some sales copy you call your homepage.
You can of course roll your own controller, routes and views for that. But there's an even simpler solution: high_voltage. This gem, made by thoughbot, makes creating static pages a breeze. Let’s get started.
We add it to our
Gemfile and then create a new directory in our
views folder, called
pages (that’s the default, you can override it, if you want).
Next, we create a new view in our
pages directory. Let’s say we wanna create an imprint page, so we call it
imprint.html.erb. If you’re using any other templating language, like slim or haml, just adapt the file ending.
Finally, go to
http://localhost:3000/pages/imprint and voila - your imprint page will show up!
One change you probably wanna do is to change the URL structure so that the pages are directly attached to the root of your website, like
example.com/imprint instead of
Create a new file in your
initializers directory called
high_voltage.rb and put the following content there:
1 2 3
HighVoltage.configure do |config| config.route_drawer = HighVoltage::RouteDrawers::Root end
Restart your rails server and you can access your page now at