Ruby类型不匹配错误

时间:2013-03-21 14:49:09

标签: ruby-on-rails ruby nameerror type-mismatch

平台:Ubuntu 12.10

在我的终端中,我正在尝试运行bundle exec rspec spec/models/user_spec.rb

我正在关注学习Web开发:Michael Hartl的Ruby on Rails教程,以测试用户。

我的代码如下:

class User < ActiveRecord::Base      
    attr_accessible :email, :name  
end

我的错误如下所示:

root@NIyi-PC:/usr/sample_app# bundle exec rspec spec/models/user_spec.rb 
Rack::File headers parameter replaces cache_control after Rack 1.5.
/usr/sample_app/app/models/user.rb:13:in `<top (required)>': superclass mismatch for class User (TypeError)

1 个答案:

答案 0 :(得分:5)

如果您已在代码中首先声明class User,则会发生此错误:

1.9.3-p374 :001 > class Bar; end
 => nil
1.9.3-p374 :002 > class Foo; end # first declaration, no superclass
 => nil
1.9.3-p374 :003 > class Foo < Bar; end # attempting to declare superclass later
TypeError: superclass mismatch for class Foo
    from (irb):3
    from /Users/Mark/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>'

VS

1.9.3-p374 :001 > class Bar; end
 => nil
1.9.3-p374 :002 > class Foo < Bar; end # first declaration, including superclass
 => nil
1.9.3-p374 :003 > class Foo; end # don't have to mention the superclass later
 => nil

有时可能难以追踪,但一个简单的起点是搜索整个项目的“班级用户”。