我的空间控制器
# frozen_string_literal: true
# Spaces controller
class Api::V1::SpacesController < ApiController
before_action :fetch_space, only: [:show]
def index
spaces = Space.all
render_collection(spaces, { name: 'spaces' }, each_serializer: SpaceShortSerializer)
end
def show
render json: @space, serializer: SpaceSerializer
end
def create
if space = current_user.spaces.create(space_params)
render_object(space, { name: 'space' }, { serializer: SpaceSerializer })
else
render_error(space.errors.full_messages)
end
end
private
def fetch_space
space_id = params[:space_id] || params[:id]
@space = Space.find(space_id)
end
def space_params
params.require(:space).permit(:name, :email, :website, :phone,
amenities_attributes: [:id, :name, :available],
meeting_rooms_attributes: [:id, :count, :capacity, pricings_attributes: [:id, :duration, :price]],
private_offices_attributes: [:id, :count, :capacity, pricings_attributes: [:id, :duration, :price]],
desks_attributes: [:id, :count, :desk_type, pricings_attributes: [:id, :duration, :price]],
operating_hours_attributes: [:id, :day, :slot])
end
end
预订控制器
class Api::V1::BookingsController < ApiController before_action :fetch_space, only: [:show] def index bookings = Booking.all render_collection(bookings, { name: 'bookings' }, each_serializer: BookingItemSerializer ) end # booking_item_params: params[:entity_type] def create if booking = current_user.bookings.create(booking_params) render_object(booking, { name: 'booking' }, { serializer: BookingSerializer }) else render_error(booking.errors.full_messages) end end private def booking_params params.require(:booking).permit(:space_id, booking_items_attributes: [:id, :entity_type, :entity_id, :count], booking_dates_attributes: [:id, :from_date, :to_date, :from_time, :to_time]) end end
空间序列化器
class SpaceSerializer < ActiveModel::Serializer attributes :id, :name, :email, :website, :phone, :owner, :available_amenities, :photos belongs_to :owner, except: :profile, serializer: OwnerSerializer has_many :meeting_rooms, serializer: MeetingRoomSerializer has_many :private_offices, serializer: PrivateOfficeSerializer has_many :desks, serializer: DeskSerializer has_many :operating_hours, serializer: OperatingHourSerializer def photos Photo::DUMMY_SPACE_IMAGES.shuffle.first(3) end end
预订序列化器
class BookingSerializer < ActiveModel::Serializer attributes :id, :user_id, :space_id, :status, :booked has_many :booking_items, serializer: BookingItemSerializer def booked object.booking_items.sum(:count) end end
预订项目序列化器
class BookingItemSerializer < ActiveModel::Serializer has_many :meeting_rooms, serializer: MeetingRoomSerializer has_many :private_offices, serializer: PrivateOfficeSerializer has_many :desks, serializer: DeskSerializer has_many :operating_hours, serializer: OperatingHourSerializer end
会议室序列化器
class MeetingRoomSerializer < ActiveModel::Serializer attributes :id, :count, :capacity, :availabitity, :photos belongs_to :booking_items, serializer: BookingItemSerializer def photos Photo::DUMMY_MEETING_ROOM_IMAGES.shuffle.first(2) end def availabitity object.count - object.booking_items.sum(:count) end end
就像在会议室序列化程序中一样,我也需要显示私人办公室和办公桌序列化程序的可用性。私人办公室序列化器
class PrivateOfficeSerializer < ActiveModel::Serializer attributes :id, :count, :capacity, :photos def photos Photo::DUMMY_PRIVATE_OFFICE_IMAGES.shuffle.first(2) end end
桌面序列化器
class DeskSerializer < ActiveModel::Serializer attributes :id, :count, :desk_type, :photos def photos Photo::DUMMY_DESK_IMAGES.shuffle.first(2) end end
错误堆栈
Completed 500 Internal Server Error in 150ms (ActiveRecord: 28.3ms | Allocations: 46623) NoMethodError (undefined method `booking_items' for #<MeetingRoom:0x0000557526c2b9b0>): app/serializers/meeting_room_serializer.rb:10:in `availabitity' app/controllers/api/v1/spaces_controller.rb:13:in `show'
MeetingRoomSerializer
中更改此行
belongs_to :booking_items, serializer: BookingItemSerializer
到
has_many :booking_items, serializer: BookingItemSerializer