how to get Rails (5, 6, or 7) to create/use hyphenated urls (instead of underscore)?

187 Views Asked by At

Although Rails defaults to underscores in urls, there are good SEO reasons to use hyphens. One pretty compelling example is that Google recognizes "/blue-socks" as the two words "blue socks" whereas "/blue_socks" is the non-word "blue_socks"... and that's sometimes important for SEO.

AFAIK Ruby doesn't make it easy to use a hyphen in a method name, therefore all Rails url helpers assume multiple-word methods and therefore routes will use underscores instead of hyphens, eg, def blue_socks.

For example a controller method blue_socks will have a path blue_socks_path and the url helper url_for expects a path with an underscore and returns a url with an underscore.

Back in 2013 there were a couple SO questions asking how to get rails to generate and use urls that are better for SEO (with hyphens) and the answers involved monkey patching Rails.

In more modern Rails, say 5 and up, is there any built-in support of hyphenated route urls?

0

There are 0 best solutions below