Skip to content
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

Closed
jamesarosen opened this issue Mar 3, 2013 · 11 comments
Closed

Cannot use optional StrongParameters with :new #9534

jamesarosen opened this issue Mar 3, 2013 · 11 comments
Milestone

Comments

@jamesarosen
Copy link
Contributor

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

class FooController < ApplicationController

  def new
    @foo = Foo.new(foo_params)
  end

  private

  def foo_params
    params[:foo] || {}
  end

end

In Rails 4, I first tried

def foo_params
  params.require(:foo).permit(:bar, :baz)
end

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

def foo_params
  params[:foo] ||= {} if params[:action] == 'new'
  params.require(:foo).permit(:bar, :baz)
end

This has the same problem (likely because :foo is neither required nor permitted by the time ||= is evaluated).

Lastly, I tried

def foo_params
  params.permit(:foo).permit(:bar, :baz)
end

That, however, always returns an empty Hash.

@jamesarosen
Copy link
Contributor Author

The solution is the rather obtuse

params.permit(:foo => [ :bar, :baz ])[:foo] ||= ActionController::Params.new({})

@senny
Copy link
Member

senny commented Mar 4, 2013

What happens when you simply do:

params.permit(foo: [:bar, :baz])

@jamesarosen
Copy link
Contributor Author

If I do that, I get back { foo: { bar: "bar value", baz: "baz value" } }. The issue is that if I use require(:foo).permit(:bar, :baz), I get back { bar: "bar value", baz: "baz value" } -- no nesting.

@jamesarosen
Copy link
Contributor Author

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]

@senny
Copy link
Member

senny commented Mar 5, 2013

@fxn should I update the documentation in the guide?

@senny
Copy link
Member

senny commented Mar 5, 2013

Another thing you could try is:

params.fetch(foo:, {}).permit(:bar, :baz)

@steveklabnik
Copy link
Member

It's also important to remember that Strong Parameters are not intended to cover every case, just make simple patterns easy.

@fxn
Copy link
Member

fxn commented Mar 5, 2013

The solution with fetch is the one I like the most, it captures the fact that :foo is optional (that is the point of fetch isn't it), and returns an empty strong hash otherwise.

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.

@senny
Copy link
Member

senny commented Mar 5, 2013

@fxn I'm on it. Will open a PR to discuss the examples.

@senny
Copy link
Member

senny commented Mar 5, 2013

I'm closing this for now as there is no bug or unexpected behaviour.

@jamesarosen
Copy link
Contributor Author

Thanks, folks :)

randaalex pushed a commit to activeby/inherited_resources that referenced this issue May 16, 2016
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
renawatson68 added a commit to renawatson68/ruby_inherited_resources that referenced this issue Sep 23, 2022
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
mikejohn857 added a commit to mikejohn857/inherited_resources that referenced this issue Nov 25, 2022
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
techguru321 added a commit to techguru321/inherited_resources that referenced this issue Dec 23, 2023
I found this problem when using InheritedResources. Here fix and explanation why rails/rails#9534
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants