Не знаю, почему это не работает? Пожалуйста, помогите.
Rails 6
class Tjedan < ApplicationRecord
has_many :termini
accepts_nested_attributes_for :termini
end
class Termin < ApplicationRecord
belongs_to :tjedan
belongs_to :barber
belongs_to :pon, class_name: 'Smjena', foreign_key: 'pon'
belongs_to :uto, class_name: 'Smjena', foreign_key: 'uto'
end
class Smjena < ApplicationRecord
has_many :barberi
has_many :termini
end
Контроллер имеет
params.require(:tjedan).permit(:status, :start_date, termini_attributes: [:id, :pon, :uto])
, а форма заполняется
= form_with(model: [:admin, tjedan], local: true) do |f|
.....
= f.fields_for :termini do |t|
= t.collection_select(:pon, Smjena.all, :id, :ime, {selected: t.object.pon.id} )
при обновлении я получаю это параметры
Parameters: {
"authenticity_token" => "HM2amibKuWBUlODuPVaKXZA9TzMYi2UTvrS38KlBwuazcRALhYwqhwaki5jB3go4CD8i+10vAI1PfTRX91xOQA==",
"tjedan" => {
"termini_attributes" => {
"0" => {
"pon" => "7", "id" => "1"
}, "1" => {
"pon" => "7", "id" => "2"
}, "2" => {
"pon" => "7", "id" => "3"
}, "3" => {
"pon" => "7", "id" => "4"
}, "4" => {
"pon" => "6", "id" => "5"
}, "5" => {
"pon" => "6", "id" => "6"
}
}
},
"commit" => "Submit",
"id" => "21"
}
Тогда я получаю эту ошибку
ActiveRecord :: AssociationTypeMismatch в Admin :: TjedniController # update Ожидается Smjena (# 70142612753520), получил "7", который является экземпляром строки (# 70142566710760)