Posted almost 12 years ago. Visible to the public. Linked content.

Slugs with FriendlyId

Gem to provide nice looking urls ("/blog/the-greatest-bug-i-never-fixed"). If you don't need anything too special (like i18n for the urls) it works as a drop-in-replacement. It basically overwrites #to_param to return the slug, and .find to search by the slug.

Make sure, everywhere you build paths, you use model_path(:id => model) instead of model_path(:id => You also need to adapt all code using something like .find_by_id. The regular .find is fine.

See the github README for installation instructions.

Don't forget to give your model an indexed string column named cached_slug. A sane configuration seems to be

class Model has_friendly_id :title, :use_slug => true, :approximate_ascii => true, :ascii_approximation_options => :german, :strip_non_ascii => true end

Then :german option converts "ü" to "ue" etc.

To build initial slugs, use the provided rake task

rake friendly_id:redo_slugs MODEL=MyModel

If slugs change over time, FriendlyId remembers the old slugs, so .find still works (only with the :use_slug => true option). You should however always redirect to the current version of the slug in your view. For this, do something like the following, possibly in a before_filter

unless redirect_to :id => @record end

Also so see the excellent guide Archive .

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Tobias Kraze
Last edit:
over 11 years ago
seo, friendly_id
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more