In Rails, why do I get the answer “204 - No content” for my / PATCH / PUT update using Active Serializers models?

This code is for UserList (user can create a list of users). This particular resource does not contain list items, but simply the list title and list type.

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 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 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

Now the update works when I PUT / PATCH ... but I get

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

About 4 months have passed since I made the rails, and then I just started to study it.

1) Does anyone know why I am not getting anything? I know this is due to my version of response_with code, but I'm not sure what exactly.

2) Can someone clarify the difference between SHOW reply_with and CREATE reply_with to me. I remember that I had a problem with this, and obviously now.

show

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

CREATE

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

a) : api : v1 , ?

b) @current_user, ?

: Serializer

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

, 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
+2

, 204. , - , .

+1

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

0
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
-1

All Articles