0

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?

caffeinescript
  • 1,365
  • 2
  • 13
  • 27

2 Answers2

1

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
Darpa
  • 396
  • 1
  • 8
  • thanks darpa, if I want to let me user have the choice create a slug, how would I go about doing that? At the moment I have it automatically set with their given name – caffeinescript Jun 23 '15 at 09:16
  • oh found this http://stackoverflow.com/questions/19989827/user-editable-slugs-with-friendly-id?rq=1 – caffeinescript Jun 23 '15 at 09:17
0
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  
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
caffeinescript
  • 1,365
  • 2
  • 13
  • 27