ROR语法错误未知属性:电子邮件

时间:2014-08-27 19:34:33

标签: ruby-on-rails

我想在用户模型中向买方添加数据,而控制台显示错误

ActiveRecord::UnknownAttributeError: unknown attribute: email
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/attribute_assignment.rb:78:in `each'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/base.rb:498:in `initialize'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/persistence.rb:44:in `new'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.19/lib/active_record/persistence.rb:44:in `create'
    from (irb):8
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in `start'
    from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

写这里是什么

u = User.create( login: "weare138", email: "readfuzza@gmail.com", password: "qwertyqwerty", password_confirmation "qwertyqwerty" )

我的用户模型

class User < ActiveRecord::Base

#devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

   attr_accessible :login, :email, :password, :password_confirmation, :remember_me

   has_one :cart
   has_many :orders
end

这是我的迁移

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :login
      t.string :email
      t.timestamps
    end
  end
end

3 个答案:

答案 0 :(得分:1)

检查您的数据库并确保用户下方有电子邮件列。如果没有,则创建一个rake迁移,将电子邮件列添加到用户。

答案 1 :(得分:1)

基本上,ActiveRecord::UnknownAttributeError告诉您正在尝试构建一个具有数据库不知道的属性的用户(尽管它已在您的模型中定义)。

答案 2 :(得分:0)

运行迁移应解决问题:

bundle exec rake db:migrate