Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails NameError:未初始化的常量“________&引用;_Ruby On Rails_Ruby_Database - Fatal编程技术网

Ruby on rails NameError:未初始化的常量“________&引用;

Ruby on rails NameError:未初始化的常量“________&引用;,ruby-on-rails,ruby,database,Ruby On Rails,Ruby,Database,我正在做家庭作业,当我试图运行railsdb:migrate时,我得到了一个错误,unitialized constant Cheeses 我想有些东西的名字错了 这是我的档案: 直奶酪/db/migrate/201804232222002奶酪.rb class CreateCheeses < ActiveRecord::Migration[5.2] def change create_table :cheeses do |t| t.string :name

我正在做家庭作业,当我试图运行
railsdb:migrate
时,我得到了一个错误,
unitialized constant Cheeses

我想有些东西的名字错了

这是我的档案:

直奶酪/db/migrate/201804232222002奶酪.rb

class CreateCheeses < ActiveRecord::Migration[5.2]
  def change
    create_table :cheeses do |t|
      t.string :name
      t.string :milk_type
      t.string :image_url
      t.string :description

      t.timestamps
    end
  end
end
class Cheese < ApplicationRecord
end
class CreateCheeses
直的\u curdin/app/models/Cheese.rb

class CreateCheeses < ActiveRecord::Migration[5.2]
  def change
    create_table :cheeses do |t|
      t.string :name
      t.string :milk_type
      t.string :image_url
      t.string :description

      t.timestamps
    end
  end
end
class Cheese < ApplicationRecord
end
class
更新:我找到了答案:|

第一行应该是
奶酪类。

class Cheeses < ActiveRecord::Migration[5.2]
  def change
    create_table :cheeses do |t|
      t.string :name
      t.string :milk_type
      t.string :image_url
      t.string :description

      t.timestamps
    end
  end
end
class Cheeses
重命名模型的文件名

stright\u curdin/app/models/Cheese.rb
stright\u curdin/app/models/Cheese.rb
。因为rails约定模型的文件名应该是小写字母

class Cheese < ApplicationRecord
end
class
是的,文件名的名称部分,在时间戳之后应该与类名匹配,这就是Rails知道调用哪个类的方式。顺便说一句,在rails中,您命名的文件名都是小写的,您的模型Cheese应该在一个名为Cheese.rb的文件中,不是Cheese.rb。也尝试使用Rails生成迁移将迁移重命名为
201804232222002\u create\u cheeses.rb可能是一个更好的主意,因为该迁移文件包含一个创建Cheese的迁移。而
Cheeses
类意味着它处理一组奶酪。稍后您可能需要在应用程序中使用该名称。您的问题是什么?迁移文件及其内容来自何处?您是否运行了rails generate
?如果是这样,您是否在以后编辑或重命名了该文件?