PoC & Documentation for new Parameters#mandate
#51674
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
I've been hunting around trying to fix the problem with the default, recommended way of handling parameters in Rails.
This is fine until someone using your app starts messing with the parameters and causing 500 errors by passing:
This causes a NoMethodError because
permit
is called on"string"
.The recommendation is not the best way. Instead, the first statement should be written like this:
However, because this is a bit messy and repetitive and the order almost seems backwards, I suggest adding a new method to params that does this all in one go and ensures that it's more likely people do it correctly.
The name is a little weird at first, and I'm open to changing it. My thinking is that a "mandate" is something that is "officially required" (something that is both permitted and required). This speaks to the actual code, which is basically just
permit(args).require(args)
with some extra behavior for hashes to require the keys.Detail
This pull request adds the
mandate
method documentation only. Please tell me if it is worth writing the tests. Forgive my hesitance, but given that many of my other recent PRs have not received any comments, I don't want to spend a bunch of time writing tests for a dead-end feature. I hope you'll understand.Additional information
Related PRs and issues that address this problem:
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]