how do I blacklist users slugs with friendly id?

286 views Asked by At

I want to use friendly id so that users can have a nice url. However I want to blacklist certain names such as api or admin or curse words. Where do I load the yaml file to do that? in the model?

2

There are 2 answers

2
Darpa On

Give the following code a try-

class MyModel < ActiveRecord::Base
  extend FriendlyId
  excluded_words = ["admin", "api"]
  friendly_id_config.reserved_words.concat(excluded_words)
  friendly_id :name, use: [:slugged, :finders]
end
0
caffeinescript On
def check_slug_blacklist
  blacklist = YAML.load_file(Rails.root.join('config/blacklist.yml'))
  if blacklist.include?(self.slug)
    self.errors.add :slug, "Please choose to a different slug"  # TO DO USE I18n
  end
end