无法添加"用户"数据模型到数据库

时间:2015-08-02 06:01:12

标签: ruby-on-rails ruby railstutorial.org

我是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

导致此错误的原因是什么?

1 个答案:

答案 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 }