rails 无法创建新项目
rails cannot create a new item
我正在创建一个小型 rails 应用程序,但我 运行 遇到了一个我认为是在我的数据库后端的问题,但我不知道如何解决它。最终发生的事情是,如果我尝试访问我的 new
url,我会收到一条错误消息 PG::UndefinedTable: ERROR: relation "caves" does not exist LINE 8: WHERE a.attrelid = '"caves"'::regclass
我想做的就是创建一个新的 cafe
我去了 localhost:3000/cafes/new
但是由于一些非常烦人的原因,洞穴不知何故混入了某个地方,我不知道该怎么做解决这个问题。
我的控制器基本上在下面。 (此时我控制器中的其余项目都是空方法)
def new
@cafe = Cafe.new
end
def create
@cafe = Cafe.new(cafe_params)
end
private
def cafe_params
params.require(:name, :description)
end
我的架构 table 是
enable_extension "plpgsql"
create_table "cafes", force: :cascade do |t|
t.string "name"
t.text "description"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.integer "age"
end
我现在有两个空模型,一个叫 user.rb
,另一个叫 cafe.rb
这是我真正感到困惑的地方,因为我的模型中没有洞穴关系。
class Cafe < ApplicationRecord
end
这就是我所在的位置。有人知道我能用这个做什么吗?我在想可能某处有一个重复的数据库,它需要被销毁。我尝试了 rake db:drop
+ rake db:create
+ rake db:migrate
路线,但没有成功。我知道有一种方法可以直接通过 psql 打开我的数据库,我认为这可能是真正调试这个问题的方法。 (看过一次做过,但是不记得怎么做了,更别提只知道active record了,不会sql)
有人知道这里发生了什么吗?或者有人知道如何进入 psql 以及如何找到这样的东西吗?
我真的很感激。
你有运行rake db:create
和rake db:migrate
吗?
因为模型名称 Cafe
的 table 名称构建是 caves
。
所以尝试 运行 rake db:create
和 rake db:migrate
。并重新启动 rails 服务器将解决您的问题。
pry(main)> 'Cafe'.tableize
=> "caves"
如果您希望 cafe
的复数解析为 cafes
而不是 rails 默认情况下
的 caves
,您可以使用变形来解决这个问题]
为此,请将此添加到您的 config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'cafe', 'cafes'
end
def new
@cafe = Cafe.new
end
def create
@cafe = Cafe.new(cafe_params)
@cafe.save
end
private
def cafe_params
params.require[:modelname].permit(:name, :description)
end
试试这个代码
我正在创建一个小型 rails 应用程序,但我 运行 遇到了一个我认为是在我的数据库后端的问题,但我不知道如何解决它。最终发生的事情是,如果我尝试访问我的 new
url,我会收到一条错误消息 PG::UndefinedTable: ERROR: relation "caves" does not exist LINE 8: WHERE a.attrelid = '"caves"'::regclass
我想做的就是创建一个新的 cafe
我去了 localhost:3000/cafes/new
但是由于一些非常烦人的原因,洞穴不知何故混入了某个地方,我不知道该怎么做解决这个问题。
我的控制器基本上在下面。 (此时我控制器中的其余项目都是空方法)
def new
@cafe = Cafe.new
end
def create
@cafe = Cafe.new(cafe_params)
end
private
def cafe_params
params.require(:name, :description)
end
我的架构 table 是
enable_extension "plpgsql"
create_table "cafes", force: :cascade do |t|
t.string "name"
t.text "description"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.integer "age"
end
我现在有两个空模型,一个叫 user.rb
,另一个叫 cafe.rb
这是我真正感到困惑的地方,因为我的模型中没有洞穴关系。
class Cafe < ApplicationRecord
end
这就是我所在的位置。有人知道我能用这个做什么吗?我在想可能某处有一个重复的数据库,它需要被销毁。我尝试了 rake db:drop
+ rake db:create
+ rake db:migrate
路线,但没有成功。我知道有一种方法可以直接通过 psql 打开我的数据库,我认为这可能是真正调试这个问题的方法。 (看过一次做过,但是不记得怎么做了,更别提只知道active record了,不会sql)
有人知道这里发生了什么吗?或者有人知道如何进入 psql 以及如何找到这样的东西吗?
我真的很感激。
你有运行rake db:create
和rake db:migrate
吗?
因为模型名称 Cafe
的 table 名称构建是 caves
。
所以尝试 运行 rake db:create
和 rake db:migrate
。并重新启动 rails 服务器将解决您的问题。
pry(main)> 'Cafe'.tableize
=> "caves"
如果您希望 cafe
的复数解析为 cafes
而不是 rails 默认情况下
caves
,您可以使用变形来解决这个问题]
为此,请将此添加到您的 config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'cafe', 'cafes'
end
def new
@cafe = Cafe.new
end
def create
@cafe = Cafe.new(cafe_params)
@cafe.save
end
private
def cafe_params
params.require[:modelname].permit(:name, :description)
end
试试这个代码