Sinatra和Post Datamapper协会

我正在使用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方法在公司和所有者之间建立关联吗?

carol_yingzi 回答:Sinatra和Post Datamapper协会

问题是它不应该是id_company,而必须是company_id,更改...

 property:id_company,Integer,:required =>true

作者

property :company_id,:required =>true

并以此方式卷曲,必须修复此错误

curl -d "owner[name]=rrr & owner[company_id]=1" http://localhost:4567/owners
本文链接:https://www.f2er.com/3153221.html

大家都在问