[英]how do I blacklist users slugs with friendly id?
I want to use friendly id so that users can have a nice url. 我想使用友好的ID,以便用户可以拥有一个不错的网址。 However I want to blacklist certain names such as api or admin or curse words. 但是我想将某些名称列入黑名单,例如api或admin或骂人的单词。 Where do I load the yaml file to do that? 我应该在哪里加载yaml文件? 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.