Model.save失败了吗?

时间:2013-07-25 19:38:51

标签: ruby rest post sinatra

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

1 个答案:

答案 0 :(得分:2)

快速选择: DataMapper(看起来你正在使用DataMapper)现在支持raise_on_save_failure;你可以把它当作 user.raise_on_save_failure 在你的保存电话之前,控制台应该显示出各种各样的爆发力。

如果save返回false,您可以转储user.errors.full_messages。有关这方面的更多信息,请点击此处。

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

我担心你没有将值传递给具有唯一索引的可空列(重置标记列)。