Ruby on Rails - 如何使用友好 ID 将用户 slug 列入黑名单



我想使用友好的id,以便用户可以拥有一个漂亮的URL。但是,我想将某些名称列入黑名单,例如 api 或管理员或诅咒词。我在哪里加载 yaml 文件来执行此操作?在模型中?

尝试以下代码

-
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  

最新更新