0

我在 Ubuntu 上使用 Rails 3.2.3,下面是我的模式文件和模型文件代码:

架构.rb

ActiveRecord::Schema.define(:version => 20120528062318) do

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
    t.string   "password_digest"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true

end

用户.rb

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  valides :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: true
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

我尝试使用以下命令从 Rails 控制台向数据库输入新记录:User.create(name: 'Hilal Agil', email: 'hilaal@me.com', password: 'welcome', password_confirmation: 'welcome' ) 我收到以下错误:

NoMethodError: undefined method `valides' for #<Class:0xaeba6cc>
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
        from /home/hilarl/workspace/twitster/app/models/user.rb:17:in `<class:User>'
        from /home/hilarl/workspace/twitster/app/models/user.rb:12:in `<top (required)>'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in `require_or_load'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in `load_missing_consta
nt'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_miss
ing'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
        from (irb):2
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
        from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'1.9.3p194 :003 > 

知道我在这里做错了什么吗?谢谢。

4

1 回答 1

5

你有一个错字。这一行:

valides :name, presence: true, length: { maximum: 50 }

应该这样说:

validates :name, presence: true, length: { maximum: 50 }

如果您查看错误消息,您会看到它告诉您您正在尝试调用一个不存在的方法名称验证。

于 2012-05-28T06:40:44.107 回答