Iqbal
Iqbal

Reputation: 246

friendly id and devise error when using reserved word

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'

Upvotes: 6

Views: 2333

Answers (5)

coopeu
coopeu

Reputation: 93

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)

Upvotes: 0

Jerome
Jerome

Reputation: 6217

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.

Upvotes: 1

Sarah Vessels
Sarah Vessels

Reputation: 31640

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.

Upvotes: 0

Rob DiCiuccio
Rob DiCiuccio

Reputation: 336

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

Upvotes: 3

Nirav Shah
Nirav Shah

Reputation: 689

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.

Upvotes: 0

Related Questions