Module: FriendlyId::Reserved

Defined in:
lib/friendly_id/reserved.rb

Overview

Reserved Words

The Reserved module adds the ability to exlude a list of words from use as FriendlyId slugs.

By default, FriendlyId reserves the words “new” and “edit” when this module is included. You can configure this globally by using FriendlyId.defaults:

FriendlyId.defaults do |config|
  config.use :reserved
  # Reserve words for English and Spanish URLs
  config.reserved_words = %w(new edit nueva nuevo editar)
end

Note that the error message will appear on the field :friendly_id. If you are using Rails’s scaffolded form errors display, then it will have no field to highlight. If you’d like to change this so that scaffolding works as expected, one way to accomplish this is to move the error message to a different field. For example:

class Person < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  after_validation :move_friendly_id_error_to_name

  def move_friendly_id_error_to_name
    errors.add :name, *errors.delete(:friendly_id) if errors[:friendly_id].present?
  end
end

Defined Under Namespace

Modules: Configuration

Class Method Summary collapse

Class Method Details

.included(model_class) ⇒ Object

When included, this module adds configuration options to the model class’s friendly_id_config.



42
43
44
45
46
47
# File 'lib/friendly_id/reserved.rb', line 42

def self.included(model_class)
  model_class.class_eval do
    friendly_id_config.class.send :include, Reserved::Configuration
    friendly_id_config.defaults[:reserved_words] ||= ["new", "edit"]
  end
end