我有一个模型,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
你可以分享完整的'CategoryCountsController'类。 –
@KirtiThorat这是完整的控制器categorycounts – Mutuma