0

我正在尝试通过 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)
4

1 回答 1

2

几个快速选项:DataMapper(看起来你正在使用 DataMapper)现在支持 raise_on_save_failure;您可以在保存调用之前将其作为 user.raise_on_save_failure 应用,并且控制台应该显示各种爆炸式的优点。

或者,如果保存返回 false,您可以转储 user.errors.full_messages。更多信息可以在这里找到。

http://www.ruby-doc.org/gems/docs/d/dm-validations-1.2.0/DataMapper/Validations/ValidationErrors.html

我担心您没有将任何值传递给具有唯一索引的可空列(重置令牌列)。

于 2013-07-25T19:52:32.180 回答