0

I have a tree-like structure of the Web Content Accessibility Guidelines (WCAG) with my model wcag_elements.

class WcagElement < ApplicationRecord
  acts_as_tree order: :position

  def to_param
    "#{id}-#{name.parameterize}"
  end
end

The tree looks something like this:

1. Perceivable
1.1 Text Alternatives
1.1.1 Non-text Content
1.2 Time-based Media
(etc.)

I even introduced another sub-level like this: 1.3.1a, 1.3.1b, etc.

This works very well. But now I want to make my URLs more use-friendly: at the moment, each element has a random ID, i.e. 1.1.1 has ID 3. A typical URL would look like this:

www.my-app.com/wcag/3-non-text-content

But I would rather have it like this:

www.my-app.com/wcag/1.1.1-non-text-content

So far, I calculated the 1.1.1 string through a method in the model, which would iterate reversely though the ancestors. But now I just store them in a field slug in the DB (their value will never change).

But I'm facing the following problem now:

Fullstops (.) don't seem to be allowed in URLs:

When entering http://localhost:3333/wcag/1.3.1 into my browser, I get a No route matches [GET] "/wcag/1.3.1" error, although I have a get "wcag/:slug", to: "wcag#show" route.

When entering http://localhost:3333/wcag/1-3-1, it will forward correctly to WcagController#show.

Is there a way to make fullstops work in this special case?

And: Should I use the slug as ID (and drop the current integer)?

I have no idea whether there's any benefit from that. I already looked into friendly_id gem, but this feels like over-engineered for my specific use case.

Thank you.

Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
  • From https://guides.rubyonrails.org/routing.html -- By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, id: /[^\/]+/ allows anything except a slash. You need to tell rails the . is OK in your routes file, something like ```get "wcag/:slug", slug: /([^\/])+?/, to: "wcag#show"``` – dbugger Dec 07 '22 at 13:21

0 Answers0