Using ActiveAdmin 2.9.0, Rails 6 and Postgres 14.6.
I have model Persona that looks like this in the schema:
create_table "personas", force: :cascade do |t|
t.string "orcid"
t.string "lastname"
t.string "firstname"
t.integer "scopi", default: [], null: false, array: true
end
The problem is with array "scopi" that is an array of integers. I can write this attribute fine in the console:
Persona.last.update(scopi: [1,2,3])
Persona.last
=> #<Persona id: 22, creator_id: 14, orcid: "1111-2222-3333-4444", lastname: "tt34", firstname: "aa", created_at: "2023-10-17 23:48:15", updated_at: "2023-10-18 01:29:42", scopi: [1, 2, 3]>
However in Active Admin, despite using the strong parameters as follows:
permit_params :firstname, :lastname, scopi: [ ]
during PATCH after editing with formtastic in ActiveAdmin resource edit view, I am getting
Unpermitted parameter: :scopi
and the resource's :scopi field is not saved. I believe that this should work because AA's manual states "Any form field that sends multiple values (such as a HABTM association, or an array attribute) needs to pass an empty array to permit_params".
I tried :scopi instead of scopi: [] in the permit_params, but any input entered e.g. 1,2,3 or [1,2,3] gets translated to an empty string and an empty array is saved. Most questions on this topic relate to arrays of nested parameters, but I am working directly with an array column in the model. Other than AA's manual I could not find a working example.
How should I construct the permit_params to make this work?
And, I can see ahead that the formtastic input may have problems converting my typed-in input to an array. Should I just use f.input :scopi, as: :string to achieve this?
And what format should I use when typing in the array?
.permit(:foo)will permit any permitted scalar value but that list does not include arrays..permit(foo: [])will permit an array of scalar values.[]to the name of the inputs. That happens automatically when you use checkboxes or selects. For exampleRack::Utils.parse_query("foo[]=1&foo[]=2&foo[]=3").