我正在使用 Ruby on Rails 制作一个简单的社交网络。我想在注册时为个人资料名称添加某些字符的限制。所以,在我的 User.rb 文件中,我有以下内容:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
# attr_accessible :title, :body
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
uniqueness: true,
format: {
with: /^[a-zA-Z0-9_-]+$/,
message: "must be formatted correctly."
}
has_many :statuses
def full_name
first_name + " " + last_name
end
end
我设置了一个测试来验证它是否有效,这就是测试的内容:
test "user can have a correctly formatted profile name" do
user = User.new(first_name: '******', last_name: '****', email: '********@gmail.com')
user.password = user.password_confirmation = '**********'
user.profile_name = '******'
assert user.valid?
结尾
当我运行测试时,我不断收到错误消息,说我的assert user.valid?
线路有问题。所以我想我在我的with: /^[a-zA-Z0-9_-]+$/
.
我得到的错误是1) Failure:
test_user_can_have_a_correctly_formatted_profile_name(UserTest) [test/unit/user_test.rb:40]:
但是在第 40 行,它有这段代码assert user.valid?
任何帮助表示赞赏:)