我想通過Sinatra應用程序的POST方法將用戶添加到數據庫。我的數據庫連接工作正常 - 我可以通過User.all.to_json
獲得。但是,當我嘗試POST時,出現一個不明原因的服務器錯誤,說我的請求失敗。從我所知道的情況來看,它與user.save
呼叫有關。我究竟做錯了什麼?Model.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)
你能發佈錯誤的確切內容嗎? – wmjbyatt
當我說'非說明'我的意思是。以下是錯誤:「處理您的請求時出錯。」並且user.save調用返回false。 – bkaiser
我希望這是一個語法或基本的結構錯誤,有更多經驗的人能夠發現。我不認爲它與數據庫連接有關,因爲我能夠毫無困難地進行GET。此外,這些列名稱(名稱,電子郵件,created_at,last_sign_in_at)是數據庫表的正確列名稱。 – bkaiser