Building human readable link building in Rails
A while ago I needed to improve the canonical links for some of our pages, specifically because we added more search parameters etc. so I made use of routes file to create some of these.
I started out with an extremely simple usecase: something like
/restaurants-with-:feature and then progressively tagged on more parameters.
So, I rolled a new app to test this out. I started out with:
Next, I planned out what attributes I wanted to have. Feature makes no sense for a restaurant. I decided on three:
- Location: I just used a dropdown for my purposes but it’d be easy enough to replace it with address, parse coordinates and use geofencing
- Cuisine: Japanese, Korean, Fusion, etc
- Tag: a descriptive attribute of the restaurant such as cheap, fancy, romantic, etc.
Then, I went through a few iterations of the link:
Next the cuisine:
Next the tag:
A couple of notes and pr0 tips:
- The brackets indicate that a parameter is optional and the link would work without that parameter being passed as well. This lets you use
built_restaurant_pathanywhere with any combination of parameters.
- Each of the segments also needs a delimiter of some sort otherwise it would all drop down into the next parameter. That’s why I couldn’t do just
(:tag-)(:cuisine-)because when :tag was absent all cuisines would be parsed as tags.
- My specific usecase was SEO link building since I didn’t want to have long queries in the URL show up in search engines.
- The most difficult part turned out to be planning the order so that the end result makes sense no matter which parameters are present – especially if some are missing – which is to say it was cake and the end result does look good.
- There is funkiness if you are to use
-as separators between categories that would end up with
//between terms, the helper would not generate leading or trailing slashes, etc. I didn’t have enough time to test all of these out but reader beware.
Get in touch
Or send me feedback. It will take less than 20 seconds.