Определенный метод 'booking_items' для # - PullRequest
0 голосов
/ 27 марта 2020

Я пытался создать API пространства бронирования. Я получаю эту ошибку. Может кто-то помочь мне с этим. Есть ли другой метод для вызова booking_items_attribute. Я строил API бронирования пространства

 NoMethodError in Api::V1::BookingsController#create
undefined method `booking_items' for #<BookingItemSerializer:0x0000558b758121c0>
Extracted source (around line #12):
10
11
12
13
14


  def booked
    self.booking_items.sum(:count)
  end
end

Сериализатор позиции бронирования

class BookingItemSerializer < ActiveModel::Serializer
  attributes  :entity_type, :count,  :booked


  belongs_to :booking,  serializer: BookingItemSerializer
  belongs_to :meeting_rooms, serializer: MeetingRoomSerializer
  belongs_to :private_offices, serializer: PrivateOfficeSerializer
  belongs_to :desks, serializer: DeskSerializer
  belongs_to :operating_hours, serializer: OperatingHourSerializer

  def booked
    self.booking_items.sum(:count)
  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

  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 BookingSerializer < ActiveModel::Serializer
  attributes :id, :user_id, :space_id, :status
  has_many :booking_items, serializer: BookingItemSerializer

end

Запрошено json

{
   "booking": {
       "space_id": 17,
       "booking_items_attributes": [
           {
               "entity_type": "PrivateOffice",
               "entity_id": 20,
               "count": 1
           },
            {
               "entity_type": "Desk",
               "entity_id": 20,
               "count": 1
           },
            {
               "entity_type": "PrivateOffice",
               "entity_id": 20,
               "count": 1
           }
       ],
        "booking_dates_attributes": [
           {
               "from_date": "2020-03-18",
               "to_date": "2020-03-18",
               "from_time": "2020-03-18 10:00:00 ",
               "to_time": "2020-03-18 14:00:00 "
           }]
   }
}

Вот как должен выглядеть вывод json

{
    "message": "success",
    "data": {
        "booking": {
            "id": null,
            "user_id": 31,
            "space_id": 17,
            "status": null,
            "booking_items": [
                {
                    "entity_type": "PrivateOffice",
                    "count": 1,
                    "booked" :1
                },
                {
                    "entity_type": "Desk",
                    "count": 1
                    "booked" :1
                },
                {
                    "entity_type": "PrivateOffice",
                    "count": 1
                    "booked" :1
                }
            ]
        }
    }
}

1 Ответ

0 голосов
/ 27 марта 2020

В вашем BookingItemSerializer,

замените ваш booked метод на

def booked
    self.booking.booking_items.sum(:count)
end
...