Friendly urls and slugs in rails

I was looking at the option of using something like acts_as_sluggable or salty_slugs, but with the new rails this is much easier.

Just put the following in your model. to_param has replaced acts_as_sluggable

class Page < ActiveRecord::Base

  def to_param
    id.to_s+'-'+title.downcase.gsub(' ', '-')
  end

end

And if you want to add css styling to the class of navigation you could do something like this

  def slug
    title.downcase.gsub(' ', '-')
  end

Comments