Cześć,
Mam takie zależności między modelami:
class Reservation < ApplicationRecord
has_one :stretch, dependent: :destroy
has_many :reservation_additional_services, dependent: :destroy
has_many :additional_services, through: :reservation_additional_services
belongs_to :user
enum status: [:submitted, :cancelled, :confirmed, :on_going, :altered, :finished]
accepts_nested_attributes_for :reservation_additional_services
accepts_nested_attributes_for :stretch
end
class Reservation::Stay < Reservation
has_and_belongs_to_many :rooms,
foreign_key: 'reservation_id',
association_foreign_key: 'room_id',
join_table: :stays_rooms
validates :room_ids, presence: true
enum board: [:without, :half, :full]
end
class ReservationAdditionalService < ApplicationRecord
belongs_to :slot
belongs_to :reservation
belongs_to :additional_service
validates :additional_service_id, presence: true
end
class Slot < ApplicationRecord
belongs_to :additional_service
has_one :reservation_additional_service
end
Odpowiednie routes’y:
resources :reservations, except: [:update]
resources :reservation_stays, controller: :stays, except: [:index, :edit, :show, :destroy]
I następujące dwa kontrolery z których korzystam(załączam tylko akcję z ktorymi jest problem):
class ReservationsController < ApplicationController
load_and_authorize_resource except: [:index]
def edit
@reservation.reservation_additional_services.build
end
end
class StaysController < ApplicationController
load_and_authorize_resource :reservation, parent: false, class: Reservation::Stay
def update
@reservation.reservation_additional_services.build unless @reservation.reservation_additional_services.any?
if @reservation.update_attributes(update_params)
@reservation.altered!
render nothing: true
else
render 'reservations/edit', status: :not_acceptable
end
end
private
def update_params
params.require(:reservation_stay).permit(
reservation_additional_services_attributes: [:additional_service_id, :slot_id]
)
end
end
i taki widok edit:
h4 = t(’.book_service’)
= simple_form_for(@reservation,
defaults: { label: false }) do |f|
.row
= f.simple_fields_for :reservation_additional_services do |rasf|
= rasf.input :slot_id, input_html: { class: 'slot-select' }
= rasf.input :additional_service_id,
as: :select, input_html: { class: 'services-select-input' }, label: t('.labels.additional_service'),
collection: AdditionalService.all.collect {|as| ["#{as.name} - #{as.price}", as.id] }
= render 'reservations/form/resources', resources: AdditionalService.all
.form-group
input type="date" class="datepicker form-control hidden" id="reservation_date"
#slots.slots-wrapper
.row
= f.submit t('.labels.book')
javascript:
Reservations.initForm('service');
Wykonuję tylko update dla Reservation::Stay
i w trakcie tego update, chcialbym umożliwić utworzenie jednego reservations_additional_services
, jednak dostaję komunikat błędu - formularz przed submit’em"
formularz po submicie z błędem:
oprócz tego błędu został stworzony dodatkowy pusty obiekt, który nie powinien powstać - powinien być ciągle jeden i ten sam reservation_additional_service
.
Paramsy:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"SBmipd2WLoYwflxlZrWzTq3AdBqsT/Z2iYgUCBXu5jbtNgZ43PELDUupX+BPQZ1uI6/z1SdQj0SSpmQI4Gg9MA==", "reservation_stay"=>{"reservation_additional_services_attributes"=>{"0"=>{"slot_id"=>"43", "additional_service_id"=>"1"}}}, "commit"=>"Zarezerwuj", "controller"=>"stays", "action"=>"update", "id"=>"1"}
i update_params:
{"reservation_additional_services_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"additional_service_id"=>"1", "slot_id"=>"43"} permitted: true>} permitted: true>}
@reservation.errors.full_messages
:
Reservation additional services slot translation missing: pl.activerecord.errors.models.reservation_additional_service.attributes.slot.required
Reservation additional services additional service translation missing: pl.activerecord.errors.models.reservation_additional_service.attributes.additional_service.required
Reservation additional services additional service nie może być puste
Aplikacja jest po polsku i nie ma przetłumaczonych wszytskich tekstów, w każdym razie wyraźnie widać, że brakuje wymaganych parametrów, które są wysyłane z requestem, więc nie wiem o co chodzi.