Why raw params can't reach .new
The safeguard two courses named
ActiveRecord: creating records and ActionController: actions both named article_params as the safe stand-in for raw params without building it. Here's what happens without it:
raw = ActionController::Parameters.new(title: "Hello Rails", admin: true)
Article.new(raw)
# ActiveModel::ForbiddenAttributesError
Rails raises right away. In a real request, params arrives as an ActionController::Parameters object, not a plain Ruby Hash, and that object starts out unable to be mass-assigned.
permitted? is the actual flag
raw.permitted?
# => false
Every ActionController::Parameters object carries this flag. .new checks it and refuses when it's false. The flag is an explicit switch that always starts off; Rails doesn't guess whether a hash looks dangerous.
Why the default is "no"
params holds whatever the request carried, which can include more than the form's visible fields, the concern ActiveRecord: creating records raised. With permitted? starting at false, a developer must decide what's allowed through (lessons 2 and 3) instead of trusting everything by accident.
Key takeaways
- Raw
paramspassed to.newraisesActiveModel::ForbiddenAttributesError. permitted?isfalseby default, and.newchecks it.- The
falsedefault forces a deliberate choice of what's allowed.
Keep going with lesson 2
.permit: naming exactly what's allowed
The .permit method returns a permitted object holding only the listed keys, which .new then accepts.
A free account also gives you the quizzes, your progress and notes, and the discussion.