📎 Webclip
Peeking Under the Hood of ActionController Parameters, Part 2
The post explains that Rails params look like a hash, but they are instances of ActionController::Parameters. That class inherits from ActiveSupport::HashWithIndifferentAccess, which lets keys be accessed with either strings or symbols, while adding strong-parameters behavior.
It also shows that new params objects start unpermitted, that permit and permit! set the @permitted attribute, and that controller params come from ActionController::Metal through a memoized params method.
Reading notes#
- Rails can feel like a black box because much of its logic is abstracted away.
- params in controllers are instances of ActionController::Parameters, not plain Ruby Hashes.
- ActionController::Parameters inherits from ActiveSupport::HashWithIndifferentAccess.
- HashWithIndifferentAccess treats :foo and “foo” as the same key and returns keys as strings.
- A Hash can be converted with with_indifferent_access.
- ActionController::Parameters objects are unpermitted by default.
- permitted? returns the @permitted attribute.
- permit calls permit! after filtering permitted scalar values.
- permit! sets @permitted to true and returns self.
- permit! can be used for mass assignment when all attributes should be allowed.
- ActionController::Base inherits from ActionController::Metal.
- ActionController::Metal is the simplest Rails controller and provides a Rack interface.
- controller params are stored in @_params through a memoized params method that reads request.parameters.
- The post notes a Rails 5 change in which ActionController::Parameters will return an object instead of a Hash instance.
