【发布时间】:2013-01-18 10:21:20
【问题描述】:
我正在尝试使用grape 创建一个 API,它会在发布时将 JSON 数据保存到 PostgreSQL。 这是示例 JSON:
{
"about": "this is about me",
"company_name": "David Co",
"company_url": "http://google.com",
"created_at": "2013-02-03T13:09:37+05:30",
"email": "jones@david.com",
"first_name": "David",
"google_plus": "http://google.com",
"id": 1
}
这是 Ruby 代码:
class Posts < Grape::API
version 'v1', :using => :path
format :json
resource 'posts' do
get "/" do
Post.all
end
get "/:id" do
Post.find(params['id'])
end
post "/create" do
Post.create(params['post'])
end
end
end
我正在关注这个sample,它适用于上述 JSON。
如何修改代码以使其适用于更复杂的 JSON,例如:
{
"about": "this is about me",
"company_name": "David Co",
"company_url": "http://google.com",
"created_at": "2013-02-03T13:09:37+05:30",
"email": "jones@david.com",
"first_name": "David",
"google_plus": "http://google.com",
"id": 1,
"chall": [{
"attributes": {
"type": "tegory__c",
"url": "/services0/sobjects/__c/a08Z0000000RmoTIAS"
}
}
这是当前的数据库架构:
create_table :posts do |t|
t.string :first_name
t.string :email
t.string :company_name
t.string :google_plus
t.string :about
t.string :company_url
t.timestamps
end
【问题讨论】:
-
如果您尝试使用更复杂的 JSON,现在会发生什么? Ruby 或 PostgreSQL 中的任何错误消息?
-
其实这行不通...我没有在 postgres 中包含这些字段。我不知道如何绘制数据库架构。我刚刚更新了当前的数据库架构
标签: ruby json postgresql grape-api