how do I blacklist users slugs with friendly id?

痴心易碎 提交于 2019-12-25 03:13:39

问题


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?


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



回答2:


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  


来源:https://stackoverflow.com/questions/30799735/how-do-i-blacklist-users-slugs-with-friendly-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!