我正在使用sinatra和datamapper构建rest api,并且我的数据库文件如下所示:
require 'data_mapper'
DataMapper.setup(:default,'sqlite::memory:')
class Company
include DataMapper::Resource
property :id,Serial
property:name,String,:required => true
property:adress,:required => true
property:city,:required => true
property:country,:required => true
property:email,String
property:phoneNumber,Numeric
has n,:owners,:constraint => :destroy
end
class Owner
include DataMapper::Resource
property :id,:required => true
property:id_company,Integer,:required =>true
belongs_to:company
end
DataMapper.finalize
DataMapper.auto_migrate!
并且我想发布一种将所有者添加到公司的方法
post '/owners'do
content_type :json
owner = Owner.new params[:owner]
if owner.save
status 201
else
status 500
json owner.errors.full_messages
end
end
但是当我尝试运行此请求时,出现此错误:
curl -d "owner[name]=rrr & owner[id_company]=1" http://localhost:4567/owners
["Company must not be blank"]
有人可以告诉我如何通过post方法在公司和所有者之间建立关联吗?