First off I'm very new to rails and ruby for that matter.
I'm trying to create a slug for custom URLS.
I have a user model with a first_name & last_name. This model also has a slug.
What I am trying to do is get it to check my database to see if there is an instance of the slug to be created eg. see how many Joe Blogs exist. If there is none it gets the url whatever.com/joe-blogs and if there more than 0 it gets the value of the count whatever.com/joe-blogs-COUNT
what I attempted to do when gerating my slug
def generate_slug
NoSlug == Users.sum("slug").where(:slug => first_name-last_name.perameterize)
if NoSlug == 0
self.slug ||= first_name-last_name.perameterize
else NoSlug > 0
self.slug ||= first_name-last_name-NoSlug.perameterize
end
end
I've tried to fix it but with no such luck - Any suggestions or pointers?
Thanks!
I have been using the FriendlyId gem and have been pleased. If you want to combine first name and last name, you could do something like
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :full_name, use: :slugged
def full_name
"#{first_name} #{last_name}"
end
end
The gem will take care of parameterizing it for you. This RailsCast might also be helpful.