Ruby on rails 轨道未按预期工作
我在负责用户注册的模型中添加了唯一性验证。用户名应该是唯一的。 几天前我在测试时创建了一个用户,我们称之为“示例家伙”。然后我从脚手架用户界面中删除了他,现在当我试图将一个新用户注册为“example guy”时,它返回的是这个名字已经被取了。 那么,我如何在不恢复到其“出生状态”的情况下从DB修复此问题,并修改控制器以实际销毁表条目呢 或者Rails正在跟踪写入DB的内容,即使是在销毁之后 Im使用omniauth标识,用户模型:Ruby on rails 轨道未按预期工作,ruby-on-rails,Ruby On Rails,我在负责用户注册的模型中添加了唯一性验证。用户名应该是唯一的。 几天前我在测试时创建了一个用户,我们称之为“示例家伙”。然后我从脚手架用户界面中删除了他,现在当我试图将一个新用户注册为“example guy”时,它返回的是这个名字已经被取了。 那么,我如何在不恢复到其“出生状态”的情况下从DB修复此问题,并修改控制器以实际销毁表条目呢 或者Rails正在跟踪写入DB的内容,即使是在销毁之后 Im使用omniauth标识,用户模型: class User < ActiveRecord::B
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid, :email
# This is a class method, callable from SessionsController
# hence the "User."
def User.create_with_omniauth(auth)
user = User.new()
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
# ADD EMAIL
user.email = auth["info"]["email"]
user.save
return user
end
end
通过从omniauth的活动记录继承的正确“标识”模型进行验证:
class Identity < OmniAuth::Identity::Models::ActiveRecord
attr_accessible :email, :name, :password_digest, :password, :password_confirmation
#attr_accessible :email, :name, :password_digest :password, :password confirmation
validates_presence_of :name
validates_uniqueness_of :name
validates_length_of :name, :in => 6..24
validates_format_of :name, :with => /^[a-z]+$/
validate :name_blacklist
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email,
:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
:message => "doesn't look like a proper email address"
#validates_presence_of :password, on: :create
#validates_presence_of :password_confirmation
validates_length_of :password, :in => 6..24
def name_blacklist
unless self.name.blank? then
case self.name
when "user", "photo", "photos",
"application", "sessions",
"identities", "home"
self.errors.add(:name, "prohibited")
end
end
end
end
class-Identity6..24的长度
验证:name,:with=>/^[a-z]的格式+$/
验证:名称\u黑名单
验证以下项的唯一性:电子邮件,:区分大小写=>false
验证电子邮件的格式,
:with=>/\A([^@\s]+)@((?:[-A-z0-9]+\)+[A-z]{2,})\z/i,
:message=>“看起来不是正确的电子邮件地址”
#验证是否存在密码,在::create上
#验证是否存在:密码确认
验证:password,:in=>6..24的长度
def name_黑名单
除非self.name.blank?然后
case self.name
当“用户”、“照片”、“照片”时,
“应用程序”、“会话”,
“身份”,“家”
self.errors.add(:name,“禁止”)
结束
结束
结束
结束
Identifications controllar manage注册表发送至omniauth并调用new.html.erb:
class IdentitiesController < ApplicationController
def new
@identity = env['omniauth.identity']
end
end
class identiescontroller
提前感谢。我不完全确定是什么原因导致了这个问题,但我遇到了一个类似的问题,就是一个基于Desive的用户的电子邮件地址 我想在不同的用户上重复使用同一封电子邮件。我将原始用户更改为其他电子邮件,但当我尝试使用“now unique”电子邮件更新第二个用户时,唯一验证失败 对使用该电子邮件的用户的查询未返回任何内容
它似乎与唯一性约束有关,因为在删除电子邮件地址后重新启动服务器,修复了问题。在控制台中写入
bundle exec rails r'UserModel.where(:username=>“example guy”)。所有“
,并在得到结果时写入。(将UserModel
替换为您自己的)返回任何内容。。。我真的很困惑添加您的模型和控制器代码的问题。
class IdentitiesController < ApplicationController
def new
@identity = env['omniauth.identity']
end
end