我是Ruby on Rails的新手,我跟随Michael Harlt的Ruby on Rails教程。我的数据模型名为User
。当我尝试在控制台中添加User
时,会生成以下错误。
ArgumentError:或者:with或:必须提供(但不能同时提供)
控制台代码:
User.create!( :name => "Hussain" , :email => "Hussain@example.com" )
用户型号代码: -
class User < ActiveRecord::Base
email_regex = /\A[\w+\-.]+@[a-z.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :key => email_regex },
:uniqueness => { :case_sensitive => false }
end
导致此错误的原因是什么?
答案 0 :(得分:5)
尝试更改此
validates :email, :presence => true,
:format => { :key => email_regex },
:uniqueness => { :case_sensitive => false }
到
validates :email, :presence => true,
:format => { :with=> email_regex },
:uniqueness => { :case_sensitive => false }