In a Rails controller, what happens if you pass params[:job_application] straight to JobApplication.new, and how do strong parameters fix it?
answer
- ActionController::Parameters, not a Hash
- permitted? starts as false
- ActiveModel::ForbiddenAttributesError
- allow-list in a private method
- unlisted keys silently dropped
basics
~10 sparams[:job_application] is an unpermitted ActionController::Parameters, so Active Record raises ActiveModel::ForbiddenAttributesError. Strong parameters fix it with an allow-list, params.expect(job_application: [...]), which returns a permitted copy holding only the listed keys.
solid answer
~40 s`params` is an `ActionController::Parameters`, and every instance starts with `permitted?` false. Active Model's mass-assignment check refuses unpermitted parameters, so `JobApplication.new(params[:job_application])` raises `ActiveModel::ForbiddenAttributesError` — an error by design, because the code never said which attributes a visitor may set. The fix is an allow-list in a private method: `params.expect(job_application: [:full_name, :email, :cover_letter])` returns a new, permitted `Parameters` with only those keys, so a forged `job_application[status]=hired` or `reviewer_id` field is dropped before it reaches the model. The method lives under `private` so it is not an action, and `create` and `update` share it. The same guard covers `update`, `assign_attributes` and `where`; converting raw params with `to_h` raises `ActionController::UnfilteredParameters` instead.
code
ruby · 16 linesclass JobApplicationsController < ApplicationController
def create
@job_application = JobApplication.new(job_application_params)
if @job_application.save
redirect_to @job_application, notice: "Application received."
else
render :new, status: :unprocessable_content
end
end
private
def job_application_params
params.expect(job_application: [ :full_name, :email, :cover_letter ])
end
endgo deeper
Recognise ForbiddenAttributesError, write a private *_params method with params.expect, and explain that unlisted keys simply never reach the model.
Explain the permitted? flag, which calls check it, why to_h raises UnfilteredParameters, and why the allow-list belongs beside the actions that own the form.
Review allow-lists against roles: keep privileged attributes out of public forms, split admin controllers or methods when different users may set different fields, and reject permit! in review.
Set the convention that every write path names its writable attributes explicitly, and decide where role-dependent attribute rules live so they stay auditable as the app grows.
## Why params is not a plain hash Every value a request carries — path segments, query string, form body, JSON body — ends up in `params`, an **`ActionController::Parameters`** object. It behaves like a hash with indifferent access (`params[:email]` and `params["email"]` are the same), but it carries one extra bit of state: **`permitted?`**, which starts as `false` for everything a request brings in. That bit exists because of **mass assignment**: passing a whole hash of attributes to `new`, `update` or `assign_attributes` in one call. A job-application form renders fields for name, email and cover letter, but nothing stops a visitor from adding `job_application[status]=hired` or `job_application[reviewer_id]=7` to the request. If the controller forwards everything, the model writes everything. ## What happens with raw params 1. The form posts `job_application[full_name]`, `job_application[email]` and a forged `job_application[status]`. 2. The action calls `JobApplication.new(params[:job_application])`. 3. Active Model's `sanitize_for_mass_assignment` sees an object that responds to `permitted?` and returns `false`. 4. It raises **`ActiveModel::ForbiddenAttributesError`**. Rails has no special status for it, so the visitor gets a 500 and the developer gets a clear message in development. The same check runs in `update`, `assign_attributes`, `create_with` and `where`, so `JobApplication.where(params[:filter])` fails the same way. ## The allow-list Strong parameters move the decision into the controller, next to the action that knows what the form is for: - A private method, conventionally `job_application_params`, declares the allowed keys. - In Rails 8 the declaration is `params.expect(job_application: [:full_name, :email, :cover_letter])`. Older code writes `params.require(:job_application).permit(...)`, which still works but is no longer what generators emit. - The call returns a **new** `Parameters` object with `permitted?` true and only the listed keys. - Keys that are not listed are **dropped**, not rejected: the request succeeds without them. | Key sent by the browser | Listed? | Reaches the model? | |---|---|---| | `full_name` | yes | yes | | `email` | yes | yes | | `status` (forged) | no | no, silently dropped | | `reviewer_id` (forged) | no | no, silently dropped | ## The hazard the allow-list guards The allow-list exists because a form is a suggestion, not a contract. Anyone can open the browser's developer tools, add an input, or replay the request with `curl` and extra fields. Typical targets on a job board: - **Workflow state** such as `status` or `shortlisted`, which should only change through a staff action. - **Ownership and assignment** such as `reviewer_id` or `job_posting_id`, which decide who sees the application. - **Timestamps and flags** such as `submitted_at` or `verified`, which other code trusts. Model validations do not help here: a forged `status` of `"hired"` is a perfectly valid value. Only the controller knows that **this** form, for **this** visitor, may set name, email and cover letter and nothing else. That is why Rails refuses unpermitted parameters at the model boundary and makes the controller say what is allowed, instead of trying to infer it from the form that was rendered. ## Other conversions follow the same rule - `to_h` and `to_hash` on unpermitted parameters raise **`ActionController::UnfilteredParameters`**. - `to_unsafe_h` returns everything, permitted or not. The name is the warning. - Reading a single scalar, such as `params[:page]`, needs no permit at all; the guard is about passing **collections** of attributes to a model. ## Where the method goes The allow-list method sits under `private` for two reasons. Public controller methods become actions, and an allow-list is not something a route should reach. More importantly, keeping it next to the actions makes the contract reviewable: a reader sees in one place exactly which attributes visitors can set. When different actions allow different keys — say, staff may set `status` from an admin controller — a separate method or a separate controller makes that explicit instead of hiding it in a conditional. ## Common mistakes - "Fixing" the error by calling `permit!`, which marks every key permitted and recreates the hole the error was warning about. - Listing `:status` or `:reviewer_id` in a public form's allow-list because the model has those columns. The allow-list describes what **this form** may set, not what the table holds. - Expecting a forged key to produce an error. With an allow-list, the request simply proceeds without it. - Building the permitted hash manually with `to_unsafe_h.slice(...)`, which bypasses the type checks strong parameters perform on nested values.
- Does a Rails controller need strong parameters to read a single value such as params[:page]?No. Reading one scalar returns the value directly, and nothing checks `permitted?` on it. The guard applies when a collection of attributes is handed to a model in one call — `new`, `create`, `update`, `assign_attributes`, `where` — because that is where a forged key could set a column the form never rendered.
- Why do Rails strong parameters drop forged keys instead of rejecting the request?The allow-list is a filter, not a validator: `permit` and `expect` build a new object from the listed keys and ignore the rest, so a stray key costs nothing and legitimate clients that send extra fields keep working. Reporting is optional — `permit` can log or raise through `action_on_unpermitted_parameters` — while validating the values that remain is the model's job.
- params is a plain Ruby Hash, so it can go straight into JobApplication.new.
- Rails ignores attributes the form did not render, so forged fields cannot be saved.
- ForbiddenAttributesError means a validation failed on the model.
- Calling permit! is the standard fix for ForbiddenAttributesError.
- The allow-list should list every column the model has.