Member-only story
Rails Routes P.2 Custom Routes
This blog is a continuation of Rails Routes P.1 Understanding Route Structure
A common task when building Rails applications is creating custom routing, so let's look at a few ways we can restructure the anatomy of our routes.rb file.
Changing the URL of a static generated page.
Let's start with the common task of routing static pages, say we generate two pages with;
rails g controller Pages about contact
This command will create a directory appropriately named Pages with two pages named ‘about’ and ‘contact’. However, what will the routes look like, well initially this..

As these pages live in the Pages directory Rails helpfully tries to rout it for us, however, you may see there is a small catch to this. The route for ‘about’ and ‘contact’ is preseeded by pages meaning each URL would start with “/pages” (hostname/pages/about) to visit ‘about’, (hostname/pages/contact) to visit ‘contact’.
However, as you can see in the example above this issue is easily rectified with ‘to:’. We the syntax get ‘about’, to: ‘pages#about’ we instruct Rails to route /about to look in the pages directory and render about, easy as that!