Philip
Philip

Reputation: 7166

Rails - superclass mismatch

Playing with Rails and controller inheritance.

I've created a controller called AdminController, with a child class called admin_user_controller placed in /app/controllers/admin/admin_user_controller.rb

This is my routes.rb

  namespace :admin do
    resources :admin_user # Have the admin manage them here.
  end

app/controllers/admin/admin_user_controller.rb

class AdminUserController < AdminController
  def index
    @users = User.all
  end
end

app/controllers/admin_controller.rb

class AdminController < ApplicationController

end

I have a user model which I will want to edit with admin privileges.

When I try to connect to: http://localhost:3000/admin/admin_user/

I receive this error:

superclass mismatch for class AdminUserController

Upvotes: 5

Views: 6675

Answers (3)

E. Sutherland
E. Sutherland

Reputation: 111

I fixed it by creating a "Dashboard" controller and an "index" def. I then edited my routes.rb thusly:

Rails.application.routes.draw do



namespace :admin do
    get '', to: 'dashboard#index', as: '/'

    resources :posts
end



end

Upvotes: 0

epsilones
epsilones

Reputation: 11609

To complete what @Intrepidd said, you can wrap your class inside a module, so that the AdminUserController class doesn't inherit twice from ApplicationController, so a simple workaround would be :

module Admin
  class AdminUserController < AdminController
    def index
      @users = User.all
    end
  end
end

Upvotes: 6

Intrepidd
Intrepidd

Reputation: 20878

This error shows up if you define two times the same class with different superclasses. Maybe try grepping class AdminUserController in your code so you're sure you're not defining it two times. Chances are there is a conflict with a file generated by Rails.

Upvotes: 9

Related Questions