为什么我的json属性在Sinatra的帖子中反弹?
我希望有人能和我分享他们的知识。我有一个小的Sinatra应用程序,我想将json数据发布到它。谢天谢地,该记录正在创建中,但没有一个属性能够创建它。我已经阅读了几个使用curl的JSON示例,但它们都产生了相同的结果。所以我觉得这是我的模特。我可以用tux fine创建记录,所以它可能不是我的模型为什么我的json属性在Sinatra的帖子中反弹?,json,sinatra,Json,Sinatra,我希望有人能和我分享他们的知识。我有一个小的Sinatra应用程序,我想将json数据发布到它。谢天谢地,该记录正在创建中,但没有一个属性能够创建它。我已经阅读了几个使用curl的JSON示例,但它们都产生了相同的结果。所以我觉得这是我的模特。我可以用tux fine创建记录,所以它可能不是我的模型 curl command: curl -XPOST -H "Content-Type: application/json" "localhost:4567/date" -d '{ "p
curl command:
curl -XPOST -H "Content-Type: application/json"
"localhost:4567/date" -d
'{ "post": { "title": "different tile here"}}'
curl output:
=> {"id":13,"title":null,"body":null,"created_at":"2014-04-21T18:13:53Z",
"updated_at":"2014-04-21T18:13:53Z"}
西纳特拉输出:
[2014-04-27T20:03:48.035710 #45360] DEBUG -- : (0.1ms) commit transaction
127.0.0.1 - - [27/Apr/2014 20:03:48] "POST /date HTTP/1.1" 200 - 0.0181
{"post":{"title":"different tile here"}}
D, [2014-04-27T20:09:32.614274 #45360] DEBUG -- : (0.1ms) begin transaction
D, [2014-04-27T20:09:32.615917 #45360] DEBUG -- : SQL (0.4ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2014-04-28 01:09:32 UTC], ["updated_at", 2014-04-28 01:09:32 UTC]]
D, [2014-04-27T20:09:32.617656 #45360] DEBUG -- : (1.5ms) commit transaction
D, [2014-04-27T20:09:32.617852 #45360] DEBUG -- : (0.1ms) begin transaction
D, [2014-04-27T20:09:32.618132 #45360] DEBUG -- : (0.0ms) commit transaction
127.0.0.1 - - [27/Apr/2014 20:09:32] "POST /date HTTP/1.1" 200 - 0.0070
D, [2014-04-27T20:09:57.796909 #45360] DEBUG -- : Post Load (0.3ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
127.0.0.1 - - [27/Apr/2014 20:09:57] "GET / HTTP/1.1" 200 2149 0.0128
app.rb
require 'sinatra'
require 'sinatra/activerecord'
require './environments'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'json'
class Post < ActiveRecord::Base
end
post "/posts" do
@post = Post.new(params[:post])
if @post.save
redirect "posts/#{@post.id}"
else
erb :"posts/create"
end
end
post '/date', :provides => 'json' do
data = JSON.parse(request.body.read)
json_data = data.to_json
content_type :json
#@post = Post.new(params)
@post = Post.create(
title: data[:title]
body: data[:body]
)
if @post.save
@post.to_json
else
halt 500
end
end
get "/posts/create" do
@title = "Create post"
@post = Post.new
erb :"posts/create"
end
get "/posts/:id" do
@post = Post.find(params[:id])
@title = @post.title
erb :"posts/view"
end
get "/" do
@posts = Post.order("created_at DESC")
@title = "Welcome."
erb :"posts/index"
end
环境.rb
configure :development do
set :database, 'sqlite3:///dev.db'
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb')
ActiveRecord::Base.establish_connection(
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: 'utf9'
)
end
自从我通过这个基本的API测试这篇文章以来,我就不再使用布局文件了。我在网上浏览了Sinatra API的几个例子,不知道我没有看到什么。我在这里俯瞰什么?这几乎就像Rails 4中的强参数,但如果是这样,为什么tux允许我创建记录?thanx,sam除了使用制表符而不是空格缩进代码之外,这看起来基本上没问题;)您尝试过在路由上使用吗?看起来您几乎解决了问题–为什么要注释掉行
#data=JSON.parse(request.body.read)
?这是上一个教程中的产物。我尝试过您的建议,感谢您提供这些建议。
configure :development do
set :database, 'sqlite3:///dev.db'
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb')
ActiveRecord::Base.establish_connection(
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: 'utf9'
)
end