簡體   English   中英

Devise在API請求中說“密碼不能為空”-Rails API

[英]Devise says 'Password Can't be Blank' on a API request - Rails API

我正在嘗試使用Rails API用Devise創建一個新用戶。 但是,當您執行URL請求時,它會顯示“密碼不能為空”,而在Rails控制台上,它可以正常工作。 以下是有關控制器和請求的一些信息。 我究竟做錯了什么?

控制器:

class Api::ClientsController < ApplicationController
    skip_before_action :verify_authenticity_token
    def index
        render json: Client.all
    end
    def show
        client = Client.find(params[:id])
        render json: client
    end
    def create
        client = Client.new(client_params)
        if client.save!
            head 200
        else
            head 500
        end
    end
    private
    def client_params
        params.require("client").permit(:name, :email, :password, :password_confirmation)
    end
end

卷曲要求:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"oi", "email":"oi2@gmail.com","password":"123456", "password_confirmation":"123456"}' http://localhost:3000/api/clients

服務器響應:

  Processing by Api::ClientsController#create as JSON
  Parameters: {"name"=>"oi", "email"=>"oi2@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "client"=>{"name"=>"oi", "email"=>"oi2@gmail.com"}}
   (0.1ms)  begin transaction
  ↳ app/controllers/api/clients_controller.rb:12
  Client Exists (0.3ms)  SELECT  1 AS one FROM "clients" WHERE "clients"."email" = ? LIMIT ?  [["email", "oi2@gmail.com"], ["LIMIT", 1]]
  ↳ app/controllers/api/clients_controller.rb:12
   (0.1ms)  rollback transaction
  ↳ app/controllers/api/clients_controller.rb:12
Completed 422 Unprocessable Entity in 7ms (ActiveRecord: 0.4ms)
ActiveRecord::RecordInvalid (Validation failed: Password can't be blank):
app/controllers/api/clients_controller.rb:12:in `create'

您的CURL請求似乎不正確。

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"oi", "email":"oi2@gmail.com","password":"123456", "password_confirmation":"123456"}' http://localhost:3000/api/clients

這不會按預期將客戶端屬性置於client端之下。 嘗試以下方法:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"client":{"name":"oi", "email":"oi2@gmail.com","password":"123456", "password_confirmation":"123456"}}' http://localhost:3000/api/clients

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM