6

I used friendly id to allow user access profile with their login name, but when I try to sign up with login name that has been reserved by the friendly Id, I got an error. It seems devise can't render the error message properly. The error just works fine when I'm using authlogic.

Friendly_id Config:

:reserved_words => ["index", "new", "users", "admin", "login", "logout", "books",
                    "administrator", "signup"],

Error:

FriendlyId::ReservedError in Devise::RegistrationsController#create
FriendlyId::ReservedError

I'm using:

gem 'devise', '1.3.4'
gem 'friendly_id', '3.2.1.1'
CRM
  • 4,569
  • 4
  • 27
  • 33
Iqbal
  • 246
  • 2
  • 10

5 Answers5

3

The following works with friendly_id 4.0.0.beta14 on Rails 3.1.1

extend FriendlyId
friendly_id :name

after_validation :validate_reserved

def validate_reserved
  if @errors[:friendly_id].present?
    @errors[:name] = "is reserved. Please choose something else"
    @errors.messages.delete(:friendly_id)
  end
end

My friendly_id.rb initializer looks like this:

FriendlyId.defaults do |config|
  config.use :slugged, :reserved
  config.reserved_words = %w(new edit index show data)
end
Rob DiCiuccio
  • 326
  • 2
  • 7
  • 1
    This can be shortened to a one liner - `errors.add :name, *errors.delete(:friendly_id) if errors[:friendly_id].present?` as documented [here](http://rubydoc.info/github/norman/friendly_id/master/FriendlyId/Reserved) – Rahul Sekhar Jun 14 '14 at 08:21
  • Inspecting these values at all will add them to the error array. Before checking to see if they are present i added `if !@errors.empty? && @errors[:friendly_id].present?` – heavysixer May 19 '15 at 16:12
1

Or simply edit the config/initilaizers/friendly_id.rb file and add your reserved words to the

config.reserved_words = %w( [...] )

block, as the documentation attests.

Jerome
  • 5,583
  • 3
  • 33
  • 76
0

You can also alter your config/locales/en.yml or appropriate language file:

en:
  activerecord:
    errors:
      models:
        your_model_name:
          attributes:
            friendly_id:
              exclusion: 'name is a reserved word'

It reads like Validation failed: Friendly name is a reserved word.

Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
0

Thanks @Jerome it fixes my error <%= link_to 'Surt', destroy_user_session_path, method: :delete %> Not route user/sign_out simply adding sign_out to friendly_id reserved words

config/initilaizers/friendly_id.rb
config.reserved_words = %w(new edit index session login logout sign_out users admin
    stylesheets assets javascripts images)
coopeu
  • 83
  • 5
0

After going through this discussion, I added following in user.rb and it seems to work fine:

after_validation :validate_reserved

def validate_reserved
        slug_text
        rescue FriendlyId::BlankError
        rescue FriendlyId::ReservedError
        @errors[friendly_id_config.method] = "is reserved. Please choose something else"
        return false
end

I have also added a condition where it will rescue FriendlyId::BlankError as I am already checking it in my normal validation.

Nirav Shah
  • 689
  • 6
  • 16