-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use optional StrongParameters with :new #9534
Comments
The solution is the rather obtuse params.permit(:foo => [ :bar, :baz ])[:foo] ||= ActionController::Params.new({}) |
What happens when you simply do: params.permit(foo: [:bar, :baz]) |
If I do that, I get back |
It may be that there is no good code solution to this in Rails and it has to be treated as a documentation issue. In that case, I recommend expressing that the following are similar params.require(:foo).permit(:bar, :baz)
params.permit(foo: [ :bar, :baz ])[:foo] |
@fxn should I update the documentation in the guide? |
Another thing you could try is: params.fetch(foo:, {}).permit(:bar, :baz) |
It's also important to remember that Strong Parameters are not intended to cover every case, just make simple patterns easy. |
The solution with Maybe we could have a little section with a cookbook to solve problems like this and others that have been reported in the guide, this is new and it would be useful to explain with examples how to use the API to solve use cases that deviate from the normal one. |
@fxn I'm on it. Will open a PR to discuss the examples. |
I'm closing this for now as there is no bug or unexpected behaviour. |
Thanks, folks :) |
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
I can't come up with a good way to use
StrongParameters
for optional parameters for a:new
action.In Rails 3, I wrote something like
In Rails 4, I first tried
That causes an
ActionController::ParameterMissing: param not found: feature
if people go to/foos/new
without providing any arguments (like?foo[bar]=varValue
).The next thing I tried was
This has the same problem (likely because
:foo
is neitherrequire
d norpermit
ted by the time||=
is evaluated).Lastly, I tried
That, however, always returns an empty Hash.
The text was updated successfully, but these errors were encountered: