0

我使用Ubuntu上的Rails 3.2.3及以下時,遇到了一個錯誤是我的架構文件和模型文件代碼:插入新記錄到數據庫

schema.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 

user.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+\-.][email protected][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:'[email protected]',密碼:'welcome',password_confirmation''歡迎'),我得到以下錯誤:

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 > 

任何想法我在做什麼錯在這裏?謝謝。

回答

5

你有一個錯字。 這條線:

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

應該這樣說:

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

如果你看一下錯誤信息,你可以看到它是在告訴你,你正試圖調用一個方法名valides,這不不存在。

相關問題