Reads:603Replies:0
Rails: Use strings like "1", "1.2", "1.2.1", and "1.2.1a" as ID values of a model
I have a tree-like structure of the Web Content Accessibility Guidelines (WCAG) with my model wcag_elements. I have started Ruby Training, But I can't able to solve this code.
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. |
|