Модель в модели. Nested forms ruby on rails ActiveAdmin
Помогите пожалуйста. Через ресурс ActiveAdmin не сохраняется вложенная модель во вложенной модели в консоли, выдает Unpermitted parameter:. Соотвественно, данные остальных моделей успешно сохраняются.
В моем случае выдает ошибку:
Ситуация с кодом:
app/admin...
ActiveAdmin.register Landing do
permit_params
performances_attributes: [:id, :title, :name, :subtitle, :img],
time_performances_attributes: [:id, :time, :block]
form name: 'Название лендинга' do |f|
inputs 'Блок программы форума' do
f.inputs do
f.has_many :performances, new_record: true, allow_destroy: true do |r|
r.input :title
r.input :name
r.input :subtitle
r.input :img
r.has_many :time_performances, new_record: true, allow_destroy: true do |rr|
rr.input :time
rr.input :block
end
end
end
end
/db/migrate/...time_performance
class CreateTimePerformances < ActiveRecord::Migration[6.0]
def change
create_table :time_performances do |t|
t.string :time
t.string :block
t.belongs_to :landing
t.belongs_to :performance
t.timestamps
end
end
end
/models/performance...
class Performance < ApplicationRecord
mount_uploader :img, ImageUploader
belongs_to :landing
has_many :time_performances
accepts_nested_attributes_for :time_performances
end
/models/time_performance...
class TimePerformance < ApplicationRecord
belongs_to :landing
belongs_to :performance
end
/models/landing
class Landing < ApplicationRecord
has_many :performances
has_many :time_performances
accepts_nested_attributes_for
:performances,
:time_performances
В обычной ситуации (контроллере), как понял, используют для передачи параметров на запись:
private
def param
атрибуты модели
end
Ответы (2 шт):
В DSL вложенных инпутов вы используете allow_destroy: true, а значит позволяете пользователю удалять вложенные записи. Под капотом в этом случае используется скрытый инпут :_destroy, который отсылается на сервер наряду с обычными полями. И его тоже нужно помещать в permit_params.
В вашем конкретном примере нужно прописать:
permit_params performances_attributes: [:id, :title, :name, :subtitle, :img, :_destroy],
time_performances_attributes: [:id, :time, :block, :_destroy]
Если у кого нибудь возникнет такая ситуация уберите привязку к основной базе (Landing)
class TimePerformance < ApplicationRecord
belongs_to :performance
end
class CreateTimePerformances < ActiveRecord::Migration[6.0]
def change
create_table :time_performances do |t|
t.string :time
t.string :block
t.belongs_to :performance
t.timestamps
end
end
end

