skip to content

In a Rails 8.1 controller, what does wrap_parameters do with a JSON body like {"full_name": "Ada"}, and which keys does it leave out?

level: middleimportance: nice to knowfreq 25%

answer

  1. JSON only, by default
  2. key from the controller's model name
  3. body params only, not the query
  4. model attribute_names decide what is copied
  5. virtual attributes are left out

basics

~20 s

For a JSON request without a job_application key, wrap_parameters copies the body's keys under job_application, so params.expect(job_application: [...]) works; with an Active Record model it copies only the model's attribute names, leaving virtual attributes out.

solid answer

~40 s

Since `load_defaults 7.0`, `wrap_parameters_by_default` is true, so every controller runs `wrap_parameters format: [:json]`. When a request's content type is JSON and the body does not already contain the wrapper key, Rails copies the **body** parameters under a key named after the controller's model — `JobApplicationsController` finds `JobApplication`, so the key is `job_application`; without a model it singularizes the controller name. The top-level keys stay too. For an Active Record model, only `attribute_names`, stored attributes, attribute aliases and `*_attributes` keys for nested associations are copied, so an `attr_accessor :referral_code` is **not** wrapped and vanishes from `params.expect(job_application: [...])`. Fix it with `wrap_parameters include: [...]`, or turn wrapping off with `wrap_parameters false`.

code

ruby · 10 lines
ruby
class JobApplicationsController < ApplicationController
  # JobApplication has attr_accessor :referral_code, not a column
  wrap_parameters include: [ :full_name, :email, :referral_code ]

  def create
    @job_application = JobApplication.new(
      params.expect(job_application: [ :full_name, :email, :referral_code ]))
    # ...
  end
end

go deeper

for a junior

Know that a flat JSON body still works with params.expect(job_application: [...]) because Rails wraps it under the model's key.

for a middle

Explain the conditions for wrapping, how the key and the copied keys are chosen from the model, and why virtual attributes go missing.

for a senior

Diagnose missing virtual attributes from JSON clients, fix them with include:, and test JSON and form clients of the same endpoint separately.

for a principal

Decide whether one endpoint should serve both HTML forms and JSON clients, or whether a separate API surface with its own parameter contract is easier to keep correct.

## The problem it solves Rails forms post nested parameters: `job_application[full_name]=Ada`. A JSON client of the same endpoint often sends a flat body: `{"full_name": "Ada", "email": "[email protected]"}`. Without help, `params.expect(job_application: [...])` would raise `ParameterMissing` for that client, and the controller would need two code paths. `ActionController::ParamsWrapper` closes the gap by **wrapping** the flat body under the key the controller expects. ## When wrapping happens Wrapping is on by default for JSON: `load_defaults 7.0` set `config.action_controller.wrap_parameters_by_default = true`, which makes the Action Controller railtie call `wrap_parameters format: [:json]` on controllers as they load. Before 7.0, the same effect came from a generated initializer, `config/initializers/wrap_parameters.rb`. For each request, the wrapper runs only when all of these hold: 1. The request has a **content type** that is in the wrapper's formats — JSON by default. 2. The controller has a **wrapper key**. 3. The parameters do **not** already contain that key, so a client that sends `{"job_application": {...}}` is left alone. ## Which key, which values - **The key** comes from the model the controller is named after. `JobApplicationsController` looks for `JobApplication` (namespaced controllers also try the namespace), and uses `job_application`. With no matching model, it singularizes `controller_name`. - **The values** come from the **request body** parameters only. Query-string parameters are not copied into the wrapper. - **The original keys remain** at the top level as well; wrapping adds, it does not move. - `authenticity_token`, `_method` and `utf8` are always excluded. - The filtered copy used for logging is wrapped too, so the request log shows the nested shape. ## What an Active Record model limits When the model is found and no `include:` or `exclude:` is given, the wrapper copies only: | Source on the model | Example | |---|---| | `attribute_names` | `full_name`, `email`, `cover_letter` | | stored attribute keys | keys declared with `store_accessor` | | attribute aliases | names from `alias_attribute` | | nested attribute writers | `references_attributes`, for associations that accept nested attributes | Anything else — a virtual `attr_accessor :referral_code`, a password field backed by a plain accessor, a key the model does not know — stays at the top level only. The allow-list then sees a `job_application` hash without it, and the value disappears without an error. If **every** body key is virtual, the wrapped hash is empty and `expect` raises `ParameterMissing`, a 400. ## Customising it - `wrap_parameters include: [:full_name, :email, :referral_code]` — copy exactly these keys. - `wrap_parameters exclude: [:internal_note]` — copy all but these. - `wrap_parameters :application, include: [...]` — choose a different key. - `wrap_parameters JobApplication` — name the model explicitly. - `wrap_parameters format: [:json, :url_encoded_form]` — wrap other content types too. - `wrap_parameters false` — switch it off for one controller. ## Diagnosing a missing JSON field When a JSON client reports that one field never saves while HTML forms work: 1. Read the request log. The `Parameters:` line shows both the top-level keys and the wrapped `job_application` hash; a key present at the top but absent from the wrapper is the wrapper's doing. 2. Check whether the key is a real attribute: `JobApplication.attribute_names.include?("referral_code")`. 3. Confirm the request's `Content-Type` is JSON. A client that sends JSON with a different content type is not wrapped at all, and the root key is then missing. 4. Fix it at the right layer: add the key to `wrap_parameters include:` for a virtual attribute, or ask the client to send the nested `job_application` object itself, which the wrapper leaves untouched. ## How it fits with strong parameters Wrapping only reshapes the parameters; it does not permit anything. The wrapped hash is still unpermitted until `expect` or `permit` filters it. Think of the wrapper as the adapter that lets one allow-list serve both HTML forms and flat JSON clients — and remember that its own filter, the model's attribute names, runs **before** yours. When a JSON field goes missing, there are two allow-lists to check, not one: the wrapper's, derived from the model, and the controller's, written by hand. Request tests that post a flat JSON body exercise both at once, which is why an API-facing endpoint deserves its own test even when the HTML form is already covered.

  • Does Rails' wrap_parameters affect a JSON request that already sends a job_application key?
    No. The wrapper runs only when the parameters do not already contain its key, so a client that nests its own body under `job_application` is left exactly as sent, and the allow-list sees the client's structure.
  • Why does wrap_parameters not copy query-string values in Rails?
    It wraps `request.request_parameters`, the parsed body, and nothing else. Values in the query string stay at the top level of `params`, so an allow-list under `job_application` will not see them unless the client moves them into the body.

  • wrap_parameters moves the body keys, so they disappear from the top level.
  • Every key in the JSON body is wrapped, including virtual attributes.
  • Wrapped parameters are permitted automatically and can go straight to the model.
  • wrap_parameters also wraps query-string parameters into the model key.
  • Rails wraps form-encoded posts by default too.

Assumes Rails 8.1 with load_defaults 8.1, which includes the 7.0 default wrap_parameters_by_default = true; apps created before 7.0 enabled the same thing in config/initializers/wrap_parameters.rb.