With Rails 8 strong parameters, how do you permit a job application's nested address, an array of skill names and a list of references?
answer
- scalars only unless declared
- key: [ ... ] for one hash
- key: [] for an array of scalars
- [[ ... ]] for an array of hashes
- hash pairs go last in the array
basics
~10 sInside params.expect, name scalar keys directly, a nested hash as address_attributes: [:street, :city], scalar arrays as skill_names: [], and arrays of hashes with double brackets, references_attributes: [[:id, :name, :_destroy]]; anything else is dropped.
solid answer
~40 sStrong parameters only pass **permitted scalars** — strings, symbols, numbers, booleans, nil, dates and times, and uploaded files — unless a key declares a nested shape. A one-to-one nested record, such as a `has_one :address` with nested attributes, is a hash: `address_attributes: [:street, :city, :postcode]`. An array of scalars needs an empty array: `skill_names: []`. An array of hashes needs **double brackets** under `expect`: `references_attributes: [[:id, :name, :phone, :_destroy]]`, which matches both a JSON array and the numeric-keyed hash (`"0" => {...}`) that form builders post. Include `:id` so existing rows update instead of duplicating, and `:_destroy` if the association allows destroying. Ruby requires the hash-style entries after the plain symbols. A key sent in a shape it was not declared with is silently left out.
code
ruby · 8 linesdef job_application_params
params.expect(job_application: [
:full_name, :email, :cover_letter, :resume,
skill_names: [],
address_attributes: [ :street, :city, :postcode ],
references_attributes: [[ :id, :name, :phone, :_destroy ]]
])
endgo deeper
Memorise the four declarations: a symbol for scalars, key: [...] for one hash, key: [] for scalar arrays, and key: [[...]] for arrays of hashes under expect.
Explain permitted scalar types, why form builders post numeric-keyed hashes, how double brackets match them, and why :id and :_destroy belong in nested record lists.
Catch silent drops in review and tests, convert single-bracket permit filters when moving to expect, and refuse {} on anything that maps to real columns.
Decide when deeply nested writes through one form are worth their allow-list complexity, versus separate endpoints or form objects with flatter, easier-to-audit parameters.
## The scenario A job-application form collects the applicant's name, email, cover letter and an uploaded résumé; a nested **address** (one record); a set of **skill names** as checkboxes or tags; and a variable list of **references**, each with a name and a phone number. The model side of nested records is configured elsewhere; this answer is about the controller's allow-list, which must describe each of those shapes exactly. ## Permitted scalars By default a declared key passes only if its value is one of the **permitted scalar types**: `String`, `Symbol`, `NilClass`, `Numeric`, `TrueClass`, `FalseClass`, `Date` (and therefore `DateTime`, a subclass), `Time`, `StringIO`, `IO`, `ActionDispatch::Http::UploadedFile` and `Rack::Test::UploadedFile`. A file field therefore needs no special syntax. If a client sends a hash or an array under a key declared as a scalar, the key is dropped. ## The four shapes | Shape in the request | Declaration in `expect` | Example | |---|---|---| | Scalar | `:key` | `:full_name`, `:resume` | | One nested hash | `key: [ ... ]` | `address_attributes: [ :street, :city, :postcode ]` | | Array of scalars | `key: []` | `skill_names: []` | | Array of hashes | `key: [[ ... ]]` | `references_attributes: [[ :id, :name, :phone, :_destroy ]]` | ## Arrays of hashes and form builders A JSON client sends references as a real array. A Rails form with nested fields sends a **numeric-keyed hash** instead: `references_attributes[0][name]`, `references_attributes[1][name]`, which parses to `{"0" => {...}, "1" => {...}}`. The double-bracket filter accepts both forms and applies the inner list to every element. With `permit`, a single-bracket filter such as `references_attributes: [:name]` is looser: it accepts a hash, an array of hashes, or a numeric-keyed hash. `expect` turns on **explicit arrays**, so under `expect` the single-bracket form accepts only a hash, and the double brackets are required for lists. ## The `*_attributes` keys The `_attributes` suffix is the key name that nested-attribute writers on the model expect. Two keys inside the element list decide how existing rows behave: - **`:id`** lets an edit update the existing reference instead of creating a new one. - **`:_destroy`** lets the form remove a reference, when the association is configured to allow it. Leaving either out is a common source of "edits duplicate rows" and "remove does nothing" bugs, and nothing raises in either case. ## Free-form hashes `key: {}` permits a hash with **any** keys, keeping scalar values and recursing into nested hashes and arrays. It suits genuinely free-form data such as a JSON settings blob. On attributes that map to columns it defeats the purpose of an allow-list. ## Ruby syntax and ordering Inside an array literal, Ruby only allows `key: value` pairs at the end, so all plain symbols come first and all hash-style entries follow. Writing `[:full_name, skill_names: [], :email]` is a syntax error, not a strong-parameters error. ## Testing nested shapes Strong-parameter declarations for nested data are easy to get subtly wrong, and every mistake is silent, so test the shapes the real clients send: - A request test that posts the numeric-keyed hash a nested form produces, then asserts the number of saved references. - A second test that posts an edit with an existing reference's `id` and asserts that no duplicate row appears. - A test that sends `_destroy: "1"` and asserts the reference is gone, if removal is allowed. - For JSON clients, the same tests with a real array, since both shapes must pass the same filter. In a console, `ActionController::Parameters.new(job_application: {...}).expect(job_application: [...])` shows exactly what survives a declaration, which is faster than reloading a form. ## Common mistakes 1. Declaring `:skill_names` without `[]`; the array is silently dropped. 2. Declaring `references_attributes: [:name]` under `expect`; the numeric-keyed hash is treated as one hash, its `"0"` and `"1"` keys are filtered out, and the references vanish. 3. Forgetting `:id` in nested record lists, so every edit creates duplicates. 4. Using `{}` on a model attribute to "make it work", which lets any key through. 5. Adding a nested key to the form without adding it to the declaration — the most common reason a nested field never saves.
- In Rails strong parameters, what does declaring settings: {} allow through?Any keys inside `settings`, as long as the values are permitted scalars, arrays of them, or nested hashes of the same kind. It is the escape hatch for genuinely free-form data such as a JSON preferences column. On ordinary attributes it reintroduces the problem the allow-list exists to solve.
- Why must :id be listed inside references_attributes: [[...]] in a Rails edit form?Without `:id` the nested writer cannot tell which existing reference each element refers to, so every submitted element is treated as a new record and an edit creates duplicates. The id is dropped silently, so nothing warns you.
- Declaring :skill_names is enough to accept an array of skill names.
- Under params.expect, references_attributes: [:name] accepts a list of references.
- Uploaded files need their own special permit syntax.
- A nested key in the wrong shape makes strong parameters raise an error.
- Using {} for each nested key is the safe way to permit nested forms.
Assumes Rails 8.1.4; the explicit double-bracket array syntax is enforced by params.expect, added in Rails 8.0; permit still treats single brackets as a hash or an array.