Member-only story

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!

Making a Custom Prefix

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Already have an account? Sign in

Steven Klavins
Steven Klavins

Written by Steven Klavins

Hi, I’m Steven, most call me Steve! I’m a programmer, musician, and artist. This blog contains various tutorials and posts related to software development.

No responses yet

Write a response