0

我有一個模型,category_count,我想屬於2個模型,一個文章模型和一個類別模型。當我嘗試訪問服務器/管理員/文章 我收到錯誤未初始化的常量Article :: CategoryCount。我正在使用active_admin進行管理。 當我做:服務器/用品/ 1/category_counts我也得到一個錯誤未初始化的常量CategoryCountsController 我使用Rails 4Rails未初始化的常量Article :: CategoryCount

這是我遷移category_count

class CreateCategoryCounts < ActiveRecord::Migration 
    def change 
    create_table :category_counts do |t| 
     t.date :date 
     t.belongs_to :category_countable, polymorphic: true 

     t.timestamps 
    end 
     add_index :category_counts, :category_countable_id 
    end 
end 

在我的模型/ article.rb和模型/ category.rb我已經設置:

的has_many:category_counts,如:category_countable

在我的模型/ category_counts.rb我設置:

belongs_to的:category_countable,多態:真

我category_count_controller.rb如下: :

class CategoryCountController < ApplicationController 
before_filter :load_category_countable 

def index 
@category_counts = @category_countable.category_counts 
end 

def new 
@category_counts = @category_countable.category_counts.new 
end 

def create 
@category_count = @category_countable.category_counts.new(params[:category_count]) 
if @category_count.save 
    redirect_to [@category_countable, :category_counts], notice: "Category Count created." 
else 
    render :new 
end 
end 

private 

def load_category_countable 
    klass= [Article, Category].detect {|c| params["#{c.name.underscore}_id"]} 
    @category_countable = klass.find(params["#{klass.name.underscore}_id"]) 
end 
end 
+0

你可以分享完整的'CategoryCountsController'類。 –

+0

@KirtiThorat這是完整的控制器categorycounts – Mutuma

回答

1

最有可能您已經定義了資源

resources :category_counts

由於路線正在尋找server/articles/1/category_counts,所以出現錯誤。 Notice 複數 ** category_counts **。 如果你在錯誤信息仔細觀察uninitialized constantCategoryCountsController, 路線正在尋找使用複數CategoryCountsController

爲了解決這個問題重命名控制器

class CategoryCountsController < ApplicationController ## plural controller name 
... 
end 

此外,請確保您重命名控制器文件名category_counts_controller.rb 又是複數。

注意:按照常規控制器名稱應爲複數。

+0

我試過了,它仍然沒有工作。我也將控制器的文件名更改爲category_counts以保證安全,但我仍然無法工作 – Mutuma

+0

讓我嘗試使用單個名稱來避免複雜性中的複雜性並查看它是否可行 – Mutuma

+0

@Mutuma您是否可以更新問題與您所做的更改。你是否得到完全相同的錯誤? –