Sinatra和后数据映射器协会



我正在用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,String,:required => true
property:city,String, :required => true
property:country,String,:required => true
property:email,String
property:phoneNumber,Numeric
has n, :owners, :constraint => :destroy
end
class Owner
include DataMapper::Resource
property :id,Serial
property:name,String, :required => true
property:id_company,Integer, :required =>true
belongs_to:company
end
DataMapper.finalize
DataMapper.auto_migrate!

我想做一个post方法,将所有者添加到公司

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

有人能告诉我如何在帖子法中建立公司和所有者之间的联系吗?

问题是它不应该是id_company必须是company_id,正在更改。。。

property:id_company,Integer, :required =>true

通过

property :company_id,Integer, :required =>true

以这种方式进行卷曲,必须修复这个错误

curl -d "owner[name]=rrr & owner[company_id]=1" http://localhost:4567/owners

最新更新