Этот код предназначен для UserList (пользователь может создать список пользователей). Этот конкретный ресурс не содержит элементы списка, а просто заголовок списка и тип списка.В Rails, почему я получаю ответ «204 - Нет содержимого» для моего обновления/PATCH/PUT, используя Active Serializers модели?
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) Кто-нибудь знает, почему я ничего не получаю? Я знаю, что это как-то связано с моей версией кода response_with, но я точно не знаю.
2) Может кто-то уточнить мне разницу между SHOW reply_with и CREATE reply_with. Я помню, что у меня была проблема с этим, и, очевидно, сейчас.
ШОУ
respond_with user_list, serializer: Api::V1::UserListSerializer
СОЗДАТЬ
respond_with :api, :v1, @current_user, user_list, serializer: Api::V1::UserListSerializer
а) Почему создание требует: апи и: v1 первый, но шоу не делает?
b) Почему для создания требуется @current_user, но показывать нет?
Приложение: Вот мой Serializer для справки
class Api::V1::UserListSerializer < ActiveModel::Serializer
attributes :id, :user_id, :type_id, :title
has_many :items, embed: :ids
end
Этот вопрос может помочь вам: http://stackoverflow.com/questions/9953887/simple-respond-with-in-rails-that-avoids- 204-from-put – Passalini