错误422无法取得的实体-Rails api vuejs来自



我正在尝试通过VUEJS表单创建新的user。我正在为后端使用Rails API。

我正在使用gem knockgem bcrypt作为JWT授权。

当我提交表格时,我在终端中遇到了422个错误。我注意到的是,它看起来像是试图将其处理为HTML,而不是JSON。

Started POST "/api/v1/users" for 127.0.0.1 at 2019-02-17 21:18:05 +0000
Processing by Api::V1::UsersController#create as HTML
  Parameters: {"email"=>"ui@gmail.com", "password"=>"[FILTERED]", "user"=>{"email"=>"ui@gmail.com"}}
Completed 422 Unprocessable Entity in 10ms (Views: 0.9ms | ActiveRecord: 0.0ms | Allocations: 576)

使用失眠时,我能够完美地创建新用户,这使我专注于前端是问题。

此外,当我从user型号中删除has_secure_password时,表格以成功的200,尽管没有密码。

为什么密码在这里播放?

createuser.vue

<template>
  <div class="card-container">
    <div class="signup">
      <form @submit.stop.prevent="newUser">
        <input id="email" v-model="email" type ="text" name="email" class="input">
        <input id="password" v-model="password" type="password" name="password" class="input">
        <input type="submit" value="Submit" class="btn-update">
      </form>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
      return {
          email: '',
          password: '',
          errors: []
      }
  },
  methods: {
  newUser() {
    axios.post('http://localhost:3001/api/v1/users', {
        email: this.email,
        password: this.password
      })
      .then(res => (this.user = res.data))
      .catch((error) => {
        console.log(error)
      });
  },
  }
};
</script>
<style>
</style>

Routes.rb

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      post 'user_token' => 'user_token#create'
      resources :users
      resources :posts
      #post   '/users/create'   => 'users#create' not making a difference
      #get    '/users/current'  => 'users#current'
    end
   end
end

users_controller.rb

class Api::V1::UsersController < ApplicationController
    before_action :authenticate_user,  only: [:index, :current, :update]
    def index 
        render json: User.all, status: 200
    end
    # Call this method to check if the user is logged-in.
    # If the user is logged-in we will return the user's information.
    def current
        current_user.update!(last_login: Time.now)
        render json: current_user
    end
    #def show
    #   @user = User.find(params[:id])
    #
    #    render json: User.find(params[:id]), status: 200
    #end
    def create
        user = User.new(user_params)
        if user.save
            render json: user, status: :created, location: user
        else
            render json: user.errors, status: :unprocessable_entity
        end
    end
    def update
        if @user.update(user_params)
          render :show, status: :ok, location: @user
        else
          render json: @user.errors, status: :unprocessable_entity
        end
    end
    def destroy
        @user.destroy
    end
private
    def set_user
        @user = User.find(params[:id])
    end
    def user_params
        params.require(:user).permit(:email, :password, :avatar)
    end
end

update

我已经在API名称空间中添加了一个默认选项。 namespace :api, defaults: {format: :json}

这已经"纠正"为JSON的处理:

Processing by Api::V1::UsersController#create as JSON

,但我仍然得到422代码。

您需要将帖子数据包装在user对象

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

您可以更改您的请求

axios.post('http://localhost:3001/api/v1/users', {
    user: {
      email: this.email,
      password: this.password
    }
  })
  .then(res => (this.user = res.data))
  .catch((error) => {
    console.log(error)
  });

最新更新