Help please. Through the ActiveAdmin resource, the nested model is not saved in the nested model in the console, it issues Unpermitted parameter :. Accordingly, the data of the remaining models are successfully saved.
In my case, it gives an error:
Situation with the code:
app / admin …
ActiveAdmin.register Landing do
permit_params
performances_attributes: [: id,: title,: name,: subtitle,: img],
time_performances_attributes: [: id,: time,: block]
form name: 'Landing page name' do | f |
inputs 'Forum program block' 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 & lt; 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 & lt; ApplicationRecord
mount_uploader: img, ImageUploader
belongs_to: landing
has_many: time_performances
accepts_nested_attributes_for: time_performances
end
/ models / time_performance …
class TimePerformance & lt; ApplicationRecord
belongs_to: landing
belongs_to: performance
end
/ models / landing
class Landing & lt; ApplicationRecord
has_many: performances
has_many: time_performances
accepts_nested_attributes_for
: performances,
: time_performances
In a normal situation (controller), as I understand, they are used to transfer parameters for writing:
private
def param
model attributes
end
Answer 1
If anyone has such a situation, remove the binding to the main database (Landing)
class TimePerformance & lt; ApplicationRecord
belongs_to: performance
end
class CreateTimePerformances & lt; 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
Answer 2, authority 100%
In the DSL of nested inputs, you use allow_destroy: true
, which means you allow the user to delete nested entries. Under the hood, in this case, the hidden input : _destroy
is used, which is sent to the server along with the usual fields. And it also needs to be placed in permit_params
.
In your specific example, you need to write:
permit_params performances_attributes: [: id,: title,: name,: subtitle,: img,: _destroy],
time_performances_attributes: [: id,: time,: block,: _destroy]