Ruby PG::不可定义:错误:关系;“城市”;第8行不存在:其中a.attrelid='&引用;“城市”';::regclass^
我是Sinatra的新手,正在尝试创建一个post请求,该请求会将一些数据添加到PGadmin中的“City”表中。但是,我在尝试添加时出错。get请求可以正常工作,在post端点上发布静态数据也可以正常工作,但是发布数据没有问题,因为迁移和city模型确实存在。代码如下: city.rbRuby PG::不可定义:错误:关系;“城市”;第8行不存在:其中a.attrelid='&引用;“城市”';::regclass^,ruby,postgresql,sinatra,rails-activerecord,Ruby,Postgresql,Sinatra,Rails Activerecord,我是Sinatra的新手,正在尝试创建一个post请求,该请求会将一些数据添加到PGadmin中的“City”表中。但是,我在尝试添加时出错。get请求可以正常工作,在post端点上发布静态数据也可以正常工作,但是发布数据没有问题,因为迁移和city模型确实存在。代码如下: city.rb class City < UserDB validates_presence_of :name, :pin def as_json(options = {}) sup
class City < UserDB
validates_presence_of :name, :pin
def as_json(options = {})
super({only: [:city, :pin]})
end
end
请注意,该路由已配置,并且在我访问url时确实有效。还运行了
db:migrate
这是Sinatra,不是Rails,对吗?@tadman是的,这是Sinatra。检查return?也在return
中:JSON:return=>@city
@thiebo{“name”=>“hamilton”,“pin”=>1234}
。参数似乎传递正确,问题可能是在数据库中创建条目时
class CreateCity < ActiveRecord::Migration[6.0]
def change
create_table :city do |t|
t.string :name
t.integer :pin
end
end
end
module Users
module V1
class CityController< LocalAPI::ApplicationBase
before do
content_type 'application/json'
end
get '/' do
city=City.all
if city
"city exists"
else
"doesn't exist"
end
end
post '/add' do
params = JSON.parse request.body.read
@name= params['name']
@pin= params['pin']
@city=City.new(name:@name,pin:@pin)
if @city.save
return output(200, @city.as_json)
else
"cant create"
end
end
end
end
end
{
"name": "hamilton",
"pin":"222"
}