質問

Platform: Ubuntu 12.10

In my terminal, I am trying to run bundle exec rspec spec/models/user_spec.rb

I am following the Learn Web Development: the Ruby on Rails tutorial by Michael Hartl to test a user.

My code is the following:

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

My error is shown below:

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)
役に立ちましたか?

解決

This error will happen if you've already declared class User elsewhere first in your code:

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

This can be difficult to track down sometimes, but an easy starting point would be to search your entire project for "class User".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top