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?
Asked
Active
Viewed 276 times
0
-
Can you please show what you have tried ? – Caffeine Coder Jun 12 '15 at 09:34
-
2friendly id has an [initializer](https://github.com/norman/friendly_id/blob/master/lib/friendly_id/initializer.rb) where you can configure reserved words. – j-dexx Jun 12 '15 at 09:34
2 Answers
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

Darpa
- 396
- 1
- 8
-
thanks darpa, if I want to let me user have the choice create a slug, how would I go about doing that? At the moment I have it automatically set with their given name – caffeinescript Jun 23 '15 at 09:16
-
oh found this http://stackoverflow.com/questions/19989827/user-editable-slugs-with-friendly-id?rq=1 – caffeinescript Jun 23 '15 at 09:17
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

MZaragoza
- 10,108
- 9
- 71
- 116

caffeinescript
- 1,365
- 2
- 13
- 27