我正在使用Michael Hartl的书来学习rails。我正在介绍有关唯一性验证的部分。我一字一句地复制它(看起来似乎),它仍然没有通过。有什么想法吗?
User.rb:
class User < ApplicationRecord
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255},
format: { with: VALID_EMAIL_REGEX},
uniqueness: case_sensitive: false
end
测试/ User_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com")
end
test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
错误:
错误:
UserTest#test_name_should_be_present:
SyntaxError: /home/benjamin/Desktop/Projects/sample_app/app/models/user.rb:6: syntax error, unexpected tLABEL
uniqueness: case_sensitive: false
^
答案 0 :(得分:3)
你需要写:
uniqueness: { case_sensitive: false }
由于
{a: b: :c}
是语法错误。
是吗:
{:a=>{:b=>:c}}
或
{{:a=>:b}=>:c}