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