Creating seo friendly url from a string
You can override the to_param
method in your model.
So if you have a model called Celebrity
, which has a name
column you can go:
class Celebrity < ActiveRecord::Base
def to_param
self.name.downcase.gsub(' ', '-')
end
end
Then:
jessica_alba = Celebrity.find_by_name("Jessica Alba")
link_to "Jessica Alba", celebrity_path(jessica_alba)
check out the has_permalink gem I've created at http://haspermalink.org
That gem will help you solve that.
The best thing is to use parameterize method:
name = name.parameterize
Inflector#parameterize