使用保留字时的友好ID和设计错误

时间:2011-05-07 18:39:56

标签: ruby-on-rails-3 devise friendly-id

我使用友好ID允许用户访问配置文件及其登录名,但是当我尝试使用友好ID保留的登录名注册时,我收到了错误消息。似乎设计无法正确呈现错误消息。当我使用authlogic时,错误正常。

Friendly_id配置:

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

错误:

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

我正在使用:

gem 'devise', '1.3.4'
gem 'friendly_id', '3.2.1.1'

5 个答案:

答案 0 :(得分:2)

以下适用于Rails 3.1.1上的friendly_id 4.0.0.beta14

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

我的friendly_id.rb初始化程序如下所示:

FriendlyId.defaults do |config|
  config.use :slugged, :reserved
  config.reserved_words = %w(new edit index show data)
end

答案 1 :(得分:0)

完成此discussion之后,我在user.rb中添加了以下内容,它似乎工作正常:

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

我还添加了一个条件,它将拯救FriendlyId :: BlankError,因为我已在正常验证中检查它。

答案 2 :(得分:0)

您还可以更改config / locales / en.yml或相应的语言文件:

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

读起来像Validation failed: Friendly name is a reserved word

答案 3 :(得分:0)

或者只需编辑config/initilaizers/friendly_id.rb文件并将您的保留字添加到

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

块,为documentation attests

答案 4 :(得分:0)

感谢@Jerome,它可以解决我的错误 <%= link_to'Surt',destroy_user_session_path,方法::delete%> 不能仅将sign_out添加到friendly_id保留字中来路由用户/ sign_out

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