我正在尝试通过 Sinatra 应用程序的 POST 方法将用户添加到数据库。我的数据库连接工作正常 - 我可以使用User.all.to_json
. 但是,当我尝试 POST 时,我收到一个无法描述的服务器错误,说我的请求失败。据我所知,这与user.save
通话有关。我究竟做错了什么?
post '/users/?' do
@request_payload = JSON.parse request.body.read
user = User.new(name: @request_payload["name"],
email: @request_payload["email"],
created_at: @request_payload["created_at"],
last_sign_in_at: @request_payload["last_sign_in_at"])
user.save
end
编辑:不确定它是否相关,但这是我连接到的数据库的架构:
Table "public.users"
Column | Type | Modifiers
------------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('users_id_seq'::regclass)
email | character varying(255) | not null default ''::character varying
encrypted_password | character varying(255) | not null default ''::character varying
reset_password_token | character varying(255) |
reset_password_sent_at | timestamp without time zone |
remember_created_at | timestamp without time zone |
sign_in_count | integer | default 0
current_sign_in_at | timestamp without time zone |
last_sign_in_at | timestamp without time zone |
current_sign_in_ip | character varying(255) |
last_sign_in_ip | character varying(255) |
name | character varying(255) |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
authentication_token | character varying(255) |
password_updated_at | timestamp without time zone |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"index_users_on_email" UNIQUE, btree (email)
"index_users_on_reset_password_token" UNIQUE, btree (reset_password_token)