如何将友好ID的用户列入黑名单?

时间:2015-06-12 09:29:50

标签: ruby-on-rails ruby-on-rails-4 gem friendly-id

我想使用友好ID,以便用户可以拥有一个不错的网址。但是我想将某些名称列入黑名单,例如api或admin或诅咒词。我在哪里加载yaml文件来做到这一点?在模型中?

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码 -

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

答案 1 :(得分:0)

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