我使用友好 ID 允许用户使用其登录名访问配置文件,但是当我尝试使用友好 ID 保留的登录名注册时,出现错误。似乎设计无法正确呈现错误消息。当我使用身份验证逻辑时,该错误工作正常。
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'
以下内容适用于 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
或者只需编辑config/initilaizers/friendly_id.rb
文件并将保留字添加到
config.reserved_words = %w( [...] )
块,正如文档所证明的那样。
经过这个讨论后,我在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,因为我已经在正常验证中检查它。
您还可以更改 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
.
谢谢@Jerome它修复了我的错误<%= link_to 'Surt', destroy_user_session_path, 方法: :d elete %>不路由用户/sign_out只需将sign_out添加到friendly_id保留字
config/initilaizers/friendly_id.rb
config.reserved_words = %w(new edit index session login logout sign_out users admin
stylesheets assets javascripts images)