发送邮件请求以使用json设计注册

时间:2018-05-22 17:45:40

标签: ruby-on-rails api post devise request

我想使用postman,rails 5和devise gem创建用户帐户。我在设计用户模型中添加了一个用户名字段,并确认它工作正常。但是,当我向Rails API发送post请求时,它不会按预期创建用户。以下是该过程的完成过程:

我将此JSON帖子请求发送到网址:http://0.0.0.0:3000/users/

{
    "username": "username",
    "email": "name@example.com",
    "password": "password",
    "password_confirmation": "password"
}

发送此请求后,我收到以下回复:

{
    "status": "ERROR",
    "message": "Could not create new user account",
    "data": {
        "email": [
            "can't be blank"
        ],
        "password": [
            "can't be blank"
        ]
    }
}

我创建了另一个继承自Devise :: RegistrationController的Registrations Controller。这是:

  1 module Api
  2   module V1
  3     class RegistrationsController < Devise::RegistrationsController
  4 
  5       before_action :configure_permitted_parameters
  6       respond_to :json

  7       def new
  8         user = User.new(user_params)
  9       end
 10 
 11       def create
 12            user = User.create(username: params[:username], 
 13                         email: params[:email], 
 14                         password: params[:password],
 15                         password_confirmation: params[:password_confirmation])
 16      
 17         if user.save 
 18           render json: {status: "SUCCESS", message: "Created new user account", data: user }, status: :ok
 19         else 
 20           render json: {status: "ERROR", 
 21                         message: "Could not create new user account", 
 22                         data: user.errors}, 
 23                         status: :unprocessable_entity
 24  
 25         end
 26       end
 27       
 28       private 
 29       
 30       def user_params
 31         params.require(:user).permit(:username, :email, :password, :password_confirmation)
 32       end
 33 
 34       protected
 35 
 36       def configure_permitted_parameters
 37         devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
 38       end
 39 
 40     end
 41   end
 42 end

知道为什么我得到这个回复,以及如何解决它?我从params哈希中抓取电子邮件和密码并将其传递给create函数。我不知道为什么我得到的回应表明它是空的

3 个答案:

答案 0 :(得分:0)

确保将邮递员的下拉菜单从text更改为JSON (application/json)

答案 1 :(得分:0)

我认为您目前遇到的问题是您没有正确访问params。如果我没有错,你的用户参数将进入params [:user]

因此您需要将此行更改为:

user = User.create(username: params[:username], 
                   email: params[:user][:email], 
                   password: params[:user][:password],
                   password_confirmation: params[:user][:password_confirmation])

但是要确定,查看服务器控制台并查看请求中的参数,您会看到类似

的内容
Started POST "/users" for 127.0.0.1 at 2018-05-22 14:56:56 -0400
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pQhz8qXPbK/lXhioL5H4iq6nxImOmtG9ya81Cuke6Ez+uqDKwNlhTMjv8spjcNhMmXFQMUAB+enlOHeZakwgow==", "user"=>{"email"=>"asdasdasasd@asdassdasd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_of_service"=>"1"}, "commit"=>"Sign up"}
#<ActionController::Parameters:0x007fa5f6b457c8>

在那里你可以看到请求中来到服务器的真实结构,谈论了参数

答案 2 :(得分:0)

您应格式化嵌套在“用户”下的参数,例如:

{  
"user": {
    "username": "username",
    "email": "name@example.com",
    "password": "password",
    "password_confirmation": "password"
  }
}

this answer上看到