简体   繁体   中英

how do I blacklist users slugs with friendly id?

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?

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
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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM