如何用Hash解决这个问题的ActiveModel :: Serializer :: Null ::

时间:2017-12-29 17:21:47

标签: ruby-on-rails json api

我正在尝试使用API​​注册学生。我正在用邮递员来测试。每当我尝试发布新学生时,这都是我的日志

Started POST "/api/v1/students/" for 127.0.0.1 at 2017-12-29 12:11:12 -0500
Processing by Api::V1::StudentsController#create as */*
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash 
(0.12ms)
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 3ms (Views: 1.1ms | ActiveRecord: 0.0ms)

Started POST "/api/v1/students/" for 127.0.0.1 at 2017-12-29 12:14:07 -0500
Processing by Api::V1::StudentsController#create as */*
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash 
(0.19ms)
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 106ms (Views: 103.7ms | ActiveRecord: 0.0ms)

这是我的控制器:

class Api::V1::StudentsController < Api::V1::BaseController

    def create
        student = Student.new(student_params)

        if student.save
            render json: {status: 'SUCCESS', message: 'Registered', data: student_data}, status: :ok
        else
            render json: {status: 'ERROR', message: 'Student not registered', data: student.errors}, status: :unprocessable_entity
        end 
    end

end

1 个答案:

答案 0 :(得分:2)

修改此

BaseController: -

before_action :authenticate_user!, except: [:new]