News By Tag
News By Location
Follow on Google News
Friendly_ID Tutorial Released for Ruby on Rails
A new tutorial has been released explaining how to set up friendly_id for Ruby on Rails. This is a free-to-use gem which has garnered a lot of support due to its simple and effective way of helping create canonical URL's (domain.com/resource).
The friendly_id gem helps build something called "canonical URLs". These translate "slugs" into id's that can be used by ActiveRecord to search for records. Although you can do this manually, the main benefit of using friendly_id is that it replaces a number of core Rails functions which would otherwise have to be manually overwritten. This not only allows for a simple transition, but also ensures you don't need to rewrite any of your core code if you decide to remove the gem.
There are two features of friendly_id which make it effective:
- Maintain your frontend route helpers
- Replaces the find/find! methods to automatically detect slugs
The way friendly_id works is to add a number of features to several core Rails functions. These features allow you to still call the same route helpers and backend methods (in the controller), thus allowing you to maintain your application's validity. The main difference is that it adds a "slug" attribute to model objects, which it basically uses to replace the "id" (primary key) column when looking up new URLs etc.
If you have a Rails application and want to use URLs such as domain.com/pages/