-
Notifications
You must be signed in to change notification settings - Fork 164
'require' should be named something like 'scope' or 'narrow' to not violate the principle of least surprise #182
Comments
Yes please. This section of the rails guides in particular totally bombs when trying to use it, because if you try it in a real app, in the http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters For the love of cake, please change the name of this method. I'm going to go make a pull request on the guides to fix that one section. |
My issue was occurring because I'd managed to create a local It's still way too easy, and confusing, to end up with an object other than the expected params hash, and accidentally calling |
Wow, totally agree here. I just spent way too long debugging this because I didn't expect that |
Probably a tangent, but the example above is actually correct. I don't know about this for sure, but I think the rationale to return the value when present is precisely to make this possible. Otherwise permitting nested hashes (nested attributes) would be a lot clumsier. |
That makes more sense, the reason it was confusing to me was because I tried to do something like I agree with this issue in general, I think clearer naming could help here. |
It's definitely confusing. Another thing that's confusing is how to require nested parameters. Say you want to ensure that params.require :foo
params[:foo].require :bar I expected to be able to chain these calls, like |
Totally agree,
Documentation is otherwise not clear on the usage and this might be as a result of being "astonishing". |
👍 It is quite confusing how params.must_have(:foo) # .require without scoping
params.may_have(:foo) # current .permit
params.scope!(:foo) # current .require
params.scope(:foo) # .permit with scoping |
The changes suggested here would constitute a massive breaking change to the vast majority of Rails applications which are using strong parameters correctly without issue. If you feel that this is a particularly important change to have in your applications it would not be particularly difficult to implement as a gem which replaced the existing behaviour of @fxn this can likely be closed. |
Could we propose other method names here which would make sense, and deprecate the old ones? I think the key request here is to distinguish the roles of "requiring/permitting" versus "shifting scope" I think |
@johnnyshields You'd have to convince @dhh or other Rails core members. In any case, you had better continue the discussion over at rails/rails as this is just a compatability gem. |
OK moved to rails/rails#19963 |
I believe the method 'require' does more than it should. It is counter-intuitive that it also FILTER the results of the statement. I expected it to do nothing more than raise an exception if the argument was not present in the target Hash.
From the API doc:
Ensures that a parameter is present. If it's present, returns the parameter at the given key, otherwise raises an ActionController::ParameterMissing error.
So, part 1 ( "Ensures that a parameter is present") makes perfect sense.
Doing anything beyond that is SURPRISING.
The fact that it ALSO does part 2, and filters my resulting map, was definitely SURPRISING. I know - RTFM. But this is rails and we don't want to violate the principle of least surprise.
I suggest that if 'require' should also FILTER the target Hash, that it be named something that gives a hint to the likelihood this may occur, like 'scope' or 'narrow'.
The text was updated successfully, but these errors were encountered: