为什么我的Rails API不将任何内容保存到数据库? [英] Why isn't my Rails API saving anything to the database?

查看:88
本文介绍了为什么我的Rails API不将任何内容保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 4.1构建一个应用程序,该应用程序具有一个Users控制器和一个单独的API控制器,用于在/v1/users处获取和发布基本用户数据(将在生产环境中的专用子域中提供).

I'm building an app with Rails 4.1 that has a Users controller and a separate API controller for getting and posting basic user data at /v1/users (will be served at a dedicated subdomain in production).

我的API :: V1:UsersController继承自ActionController :: Base,并具有以下创建方法:

My API::V1:UsersController inherits from ActionController::Base and has the following create method:

# POST /v1/users
def create
  @user = User.create(params[:user])
  if @user.save
    render :json => @user
  else
    render :json => { :errors => @user.errors.full_messages }, :status => 422
  end
end

在我的主要UsersController中,create方法如下所示:

In my main UsersController, the create method looks like this:

def create
  @user = User.new(user_params)

  @user.save
  redirect_to @user
end

private
def user_params
  params.require(:user).permit(:first_name, :last_name, :email, :birthdate)
end

...而route.rb则将所有内容捆绑在一起,如下所示:

...And routes.rb ties everything together like so:

namespace :api, :path => "/", defaults: { format: 'json' } do
  namespace :v1 do
    resources :users
  end
end

resources :users

但是,当我尝试通过curl或其他脚本将内容类型:application/json张贴到http://localhost/v1/users时,新的用户对象将以JSON返回,但是没有任何内容保存到Postgres >(我将UUID用作users表的主键).用户记录已创建,但除时间戳外,散列完全为空:

However, when I try to POST via curl or another script to http://localhost/v1/users, with Content-Type: application/json, the new user object is returned in JSON, but nothing is saved to Postgres (I'm using UUID as primary key for the users table). The user record is created, but the hash is completely empty save for timestamps:

{
    "id": "c63b559d-edf3-454d-bcb7-a0b8e5c04c10",
    "first_name": null,
    "last_name": null,
    "email": null,
    "birthdate": null,
    "created_at": "2014-06-04T23:45:15.046Z",
    "updated_at": "2014-06-04T23:45:15.046Z"
}

developing.log中我的控制台输出如下所示:

My console output in development.log looks like the following:

Started POST "/v1/users" for 127.0.0.1 at 2014-06-04 19:32:28 -0400
Processing by API::V1::UsersController#create as JSON
  Parameters: {"{ \"email\": \"test@example.com\" }"=>nil}
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.3ms)[0m  INSERT INTO "users" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2014-06-04 23:32:28.105402"], ["updated_at", "2014-06-04 23:32:28.105402"]]
  [1m[36m (0.9ms)[0m  [1mCOMMIT[0m
  [1m[35m (0.1ms)[0m  BEGIN
  [1m[36m (0.1ms)[0m  [1mCOMMIT[0m
Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 1.4ms)

我这里缺少什么吗?看起来参数甚至没有被create方法接收,并且它们肯定不会进入psql.任何帮助将不胜感激!

Is there something I'm missing here? It looks like the parameters aren't even being received by the create method, and they're certainly not making their way to psql. Any help would be appreciated!

推荐答案

json格式错误.试试这个:

The json has wrong format. Try this:

{"user":{
    "first_name": "string",
    "last_name": "string",
    "email": "string",
    "birthdate": "string",
}}

只要params.require(:user)需要对象名称.

As long as the params.require(:user) is expecting the object name.

轨道JSON请求未正确解析为发布参数

这篇关于为什么我的Rails API不将任何内容保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆