在 Rails 中,为什么我使用 Active Model Serializers 获得 update/PATCH/PUT 的"204 - No Content"响应?



此代码用于UserList(用户可以创建用户待办事项列表)。这个特定的资源不包含列表项,只包含列表的标题和列表的类型。

class Api::V1::UserListsController < ApplicationController
    respond_to :json
    skip_before_filter :verify_authenticity_token
    def index
        if authenticate_user
            user_lists = @current_user.user_lists
            if user_lists
                respond_with user_lists, each_serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's lists."}, status: :not_found
            end 
        else
            render json: { error: "User is not signed in." }, status: :unauthorized
        end     
    end         
    def show
        if authenticate_user
            user_lists = @current_user.user_lists
            user_list = user_lists.find_by_id(params[:id])
            if user_list
                respond_with user_list, serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's list."}, status: :not_found
            end 
        else
            render json: { error: "User is not signed in." }, status: :unauthorized
        end     
    end     
    def create
        if authenticate_user
            user_list = @current_user.user_lists.new(user_list_params)
            if (user_list.save!)
                respond_with :api, :v1, @current_user, user_list, serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not create new User List."}, status: :unprocessable_entity
            end         
        else
            render json: { error: "User is not signed in." }, status: :unauthorized
        end
    end
    def update
        if authenticate_user
            user_list = @current_user.user_lists.find_by_id(params[:id])
            if (user_list.update_attributes(user_list_update_params))
                respond_with :api, :v1, @current_user, user_list, serializer: Api::V1::UserListSerializer                                  
                                    #respond_with user_list, serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not update User List." }, status: :unprocessable_entity
            end
        end
    end
    private
        def user_list_params
            params.require(:user_list).permit(:user_id, :type_id, :title)
        end
        def user_list_update_params
            params.require(:user_list).permit(:type_id, :title)
        end
end

现在,当我PUT/PATCH。。。但是我有

Completed 204 No Content in 24ms (ActiveRecord: 4.3ms)

我已经有4个多月没有做任何轨道了,那时我才刚刚开始学习。

1)有人知道我为什么什么都没拿回来吗?我知道这与我在更新中的responsd_with代码行有关,但我不确定具体是什么。

2) 有人能向我澄清一下SHOW responsd_with和CREATE responsd_with之间的区别吗。我记得当时和现在都有一个问题。

显示

respond_with user_list, serializer: Api::V1::UserListSerializer

创建

respond_with :api, :v1, @current_user, user_list, serializer: Api::V1::UserListSerializer

a)为什么create首先需要:api和:v1,而show不需要?

b)为什么create需要@current_user,而show不需要?

附录:这是我的序列化程序以供参考

class Api::V1::UserListSerializer < ActiveModel::Serializer
  attributes :id, :user_id, :type_id, :title
  has_many :items, embed: :ids
end

我知道这已经晚了2年,但经过一番挖掘,我发现204的空响应是故意的(如上所述)。如果您使用respond_with,情况将始终如此。解决方法是使用render(以下示例):

class Api::V1::ItemsController < ApplicationController
  respond_to :json
  ...
  def update
    @item = Item.find(params[:id]
    if @item
      @item.update_attribute(item_params)
      render json: @item
    end
  end
  ...
end

除了204之外,您不应该得到任何其他东西。任何智能客户端都不需要接收回它刚刚发送给您的数据——它只需要确认数据是持久的。

不要错误地将类Api::V1::UserListSerializer作为键/值对(哈希形式)传递。您将得到一个错误,其中包括所需的文本类或模块。它应该是这样的:

serialize :some_array, Api::V1::UserListSerializer

或者,也许更清楚的是:

serialize(:some_array, Api::V1::UserListSerializer)

您错过了一个参数,并且正在呈现一个没有内容的对象类:204 - No Content这可能看起来很明显,但通常习惯于将事物作为键/值对传递。

一个改进:

before_action :authenticate_user, only: [:create, :show, :update, ...]

https://apidock.com/rails/ActiveRecord/Base/serialize/class

def update
  @item = Item.find(params[:id])
  respond_with(:api, :v1, @item) do |format|
    if @item.update(item_params)
      format.json { render json: @item}
    else
      format.json { render json: {error: @item.errors.full_messages}}
    end
  end
end

最新更新