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 PG::不可定义:错误:关系;“城市”;第8行不存在:其中a.attrelid='&引用;“城市”';::regclass^_Ruby_Postgresql_Sinatra_Rails Activerecord - Fatal编程技术网

Ruby PG::不可定义:错误:关系;“城市”;第8行不存在:其中a.attrelid='&引用;“城市”';::regclass^

Ruby 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

我是Sinatra的新手,正在尝试创建一个post请求,该请求会将一些数据添加到PGadmin中的“City”表中。但是,我在尝试添加时出错。get请求可以正常工作,在post端点上发布静态数据也可以正常工作,但是发布数据没有问题,因为迁移和city模型确实存在。代码如下:

city.rb

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"
}