无法通过Postman在Rails api上创建新的用户记录

时间:2016-09-15 13:25:09

标签: ruby-on-rails json devise postman

我正在通过Ruby on Rails创建新的api应用程序。 所以我使用Devise来管理我的用户表。

当我通过Postman发送请求来测试api时出现问题,它无法通过我的模型验证。

所以这是我的控制器

class UsersController < ApplicationController

  respond_to :json

  def create
    user = User.new(user_params)
    if user.save
      render json: user, status: 201, location: [:my, user]
    else
      render json: { errors: user.errors }, status: 422
    end
  end

  private

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation,:username,:fullname,:grade)
  end

end

这是我通过Postman发送给服务器的数据

{
    "user"  : {
        "email": "test5@gmail.com",
        "password": "123456",
        "password_confirmation": "123456",
        "username": "yofoyf",
        "fullname": "narotuo sarp",
        "grade": "aaa"
    }
}

我还将标题设置为application / json,如此

enter image description here

但是当我点击发送时我得到了错误,因为它无法通过我的验证

Sign up

3 errors prohibited this user from being saved:

Username can't be blank
Grade can't be blank
Fullname can't be blank

这是我的模特

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :username, presence: true ,uniqueness: true
  validates :grade, presence: true
  validates :fullname, presence: true

end

那么我怎样才能使邮递员工作? 看起来服务器无法读取我的用户名,姓名和成绩记录。

谢谢!

2 个答案:

答案 0 :(得分:0)

你已经像postlow

一样逐个发送邮递员身体部分的参数
user[:email] "test5@gmail.com"  // then in next line 
user[:password] 123456

对于每条记录,您必须使用新请求传递参数并点击发送按钮

答案 1 :(得分:0)

尝试像这样发布您的用户:

{
    "email": "test5@gmail.com",
    "password": "123456",
    "password_confirmation": "123456",
    "username": "yofoyf",
    "fullname": "narotuo sarp",
    "grade": "aaa"
}