【问题标题】:How do I solve this issue of ActiveModel::Serializer::Null::with Hash如何解决 ActiveModel::Serializer::Null::with Hash 的这个问题
【发布时间】:2017-12-29 17:21:47
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: ruby-on-rails json api


    【解决方案1】:

    修改这个

    基础控制器:-

    before_action :authenticate_user!, except: [:new]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2023-01-24
      相关资源
      最近更新 更多