I have a basic Page
model in Rails
that I'm using with FriendlyId to allow admins to create pages like "/about"
or "/contact"
.
I have the following in my routes file (config/routes.rb
) to ensure that the slugs for each page appear at the root of the site, such as https://example.com/about, etc:
resources :pages, except: [:show]
resources :pages, only: [:show], path: "/"
The problem is, with this approach, I can't use the normal named route like page_path(@page)
in my views(or tests or controllers for that matter) because that routes to "/pages/about"
and I get a "No route matches [GET] pages/about" error.
I could do both routes in my routes file so that "/pages/about"
and "/about"
work like this:
resources :pages
resources :pages, only: [:show], path: "/"
But, that creates an SEO
duplicate content problem. I suppose I could create a helper that sets the rel="canonical"
url for each page in the html
header, but that also feels like a hack. I'd prefer for there to just be 1 version of each page at the root and also have a named route such as "page_path"
or "root_page_path"
that I can use throughout my app.
The hack I've come up with for the time being is <%= link_to "#{page.slug}" %>
, but not having a named route seems very brittle.
What is a more "correct" way to do this in Rails
?
I expected something like this to work:
resources :pages, only: [:show], path: "/", as: "page"
But that doesn't work either. Nothing in the Rails guide on routing is really helping either.