namespacing管理控制器时类的超类不匹配

时间:2014-05-01 18:15:09

标签: ruby-on-rails ruby-on-rails-4

嘿我试图命名一堆我只希望管理员能够访问的控制器。例如,我想要管理员/产品或管理员/类别等路线,但当我调用位于我的控制器/管理员文件夹中的任何控制器时,我收到以下错误消息

superclass mismatch for class CategoriesController

如果我马上重新启动服务器,我会得到这个

Unable to autoload constant Admin::CategoriesController

Circular dependency detected while autoloading constant Admin::CategoriesController

这些是我的路线

Rails.application.routes.draw do
  root 'pages#home'

  devise_for :admins
  namespace :admin do
    resources :categories, :except => [:new, :show]
    resources :products
  end

  resources :products
  resources :carts, :only => [:show]
  resources :line_items, :only => [:create, :destroy]

  # Shop controller
  get 'shop/index    

  # Admin controller
  get 'admin/index'

这是我的类别控制器

class CategoriesController < ApplicationController
    before_filter :authenticate_admin!

    def index
        @categories = Category.all
        @category = Category.new
    end

    def create
        category = Category.new(categories_params)
        if category.save
        flash[:notice] = "You have added a new category"
        redirect_to categories_path
    else
        flash[:error] = "An error occured"
        render "index" 
    end
    end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    @category = Category.find(params[:id])
    if @category.update(categories_params)
      flash[:notice] = "Succesfully updated #{@category[:name].titleize}"
      redirect_to categories_path
    else
      flash[:error] = "An error occured trying to update #{@category[:name].titleize}"
      render "edit"
    end
  end

  def destroy
    @category = Category.find(params[:id])
    if @category.destroy
      flash[:notice] = "You succesfully removed #{@category.name}"      
    else 
      flash[:error] = "An error occured trying to remove #{@category.name}"
    end
    redirect_to categories_path
  end

  private

    def categories_params
            params.require(:category).permit(:name)
        end

end

这一直困扰着我一段时间,所以任何帮助都会受到高度赞赏

1 个答案:

答案 0 :(得分:3)

您忘记了namespace Controller类:

class Admin::CategoriesController < ApplicationController

这就是为什么你收到Unable to autoload constant Admin::CategoriesController错误,因为Rails正在寻找命名空间的类Admin::CategoriesController而你所拥有的是CategoriesController