Rails slugs in URL – using Active Record Model Post’s Title attribute instead of ID

This isn’t necessarily a direct answer to your question, but have you looked at the Stringex plugin (http://github.com/rsl/stringex)? It’s a great way to auto-create slugs for your records.

You can just add something like the following to your model:

class Post < ActiveRecord::Base
  acts_as_url :title
end

and it will auto-create slugs from your title and save it to the slug column.

It’s also really smart about the way it creates slugs. For example, a title of “10% off, today only” gets turned into “10-percent-off-today-only”.

Pretty slick!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)