rake aborted! TypeError: Parts is not a class

rake aborted! TypeError: Parts is not a class

好吧,这次我的数据库出现了另一个错误,说我创建的 "Parts" 中的一个 class 不是 class。我似乎不知道这是从哪里追踪的

这是我的零件数据库值(文件名为 parts.rb)

class Parts < ActiveRecord::Migration
  def change
    create_table :parts do |t|
      t.string :name
      t.text :description
      t.integer :category_id
    end
  end
end

我的零件控制器:

class PartsController < ApplicationController
  before_filter :authorize, :except => :index

  def index
    @parts = Part.all
  end

  def new
    @part = Part.new
  end

  def show
    @part = Part.find(params[:id])
  end

  def create
    @part = Part.new(part_params)
      if @part.save
        redirect_to part_path(@part)
  end
end

  def edit
    @part = Part.find(params[:id])
  end

  def update
    @part = Part.find(params[:id])
      if @part.update_attributes(part_params)
        redirect_to @part
      end
    end

    def destroy
      @part = Part.find(params[:id])
      @part.destroy
        redirect_to parts_path
    end

  private
  def part_params
    params.require(:part).permit(:description, :name)
  end

end

我的零件模型只是

class Part < ActiveRecord::Base
end

感谢您的帮助

它可能不喜欢 'Parts',因为 class 名称与模型或控制器中的某些内容冲突。或者你在某处定义了一个 Parts 模块?

我建议将迁​​移 class 名称更改为 CreateParts,即

def CreateParts < ActiveRecord::Migration
  def change
     …
  end
end

我也会更改文件名以防万一 (2016…09_create_parts.rb)

希望大家通过类似

的命令生成带模型的迁移文件
bundle exec rails g model Part name:string description:text category_id:integer

它将创建迁移文件2016...09_create_parts.rb

它看起来像

def CreateParts < ActiveRecord::Migration
  def change
   create_table :parts do |t|
      t.string :name
      t.text :description
      t.integer :category_id
    end
  end
end