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

Permitting arrays within a hash in strong_parameters #10319

Closed
wants to merge 1 commit into from

Conversation

simonc
Copy link
Contributor

@simonc simonc commented Apr 23, 2013

See: rails/strong_parameters#121

When params was of the form

    {
      :book => {
        :authors_attributes => {
          :'0' => ['William Shakespeare', '52'],
          :'1' => ['Unattributed Assistant']
        }
      }
    }

And its corresponding filter was

    {
      :book => {
        :authors_attributes => {
          :'0' => [],
          :'1' => []
        }
      }
    }

It resulted in params looking like this

    {
      :book => {
        :authors_attributes => {
          :'0' => nil,
          :'1' => nil
        }
      }
    }

This was due to the way hashes with all numeric keys are treated.
I just added a special case when a corresponding filter exists and is an empty
array.
@fxn
Copy link
Member

fxn commented Apr 24, 2013

Please watch this thread before applying: rails/strong_parameters#121 (comment).

simonc referenced this pull request in simonc/strong_parameters Aug 16, 2013
When params was of the form

    {
      :book => {
        :authors_attributes => {
          :'0' => ['William Shakespeare', '52'],
          :'1' => ['Unattributed Assistant']
        }
      }
    }

And its corresponding filter was

    {
      :book => {
        :authors_attributes => {
          :'0' => [],
          :'1' => []
        }
      }
    }

It resulted in params looking like this

    {
      :book => {
        :authors_attributes => {
          :'0' => nil,
          :'1' => nil
        }
      }
    }

This was due to the way hashes with all numeric keys are treated.
I just added a special case when a corresponding filter exists and is an empty
array.
@simonc
Copy link
Contributor Author

simonc commented Oct 18, 2013

Is this issue fixed by #12293 ?

@repinel
Copy link
Member

repinel commented Jun 21, 2015

Yes, it seems that it was fixed.

test "fields_for-style nested params" do
params = ActionController::Parameters.new({
book: {
authors_attributes: {
:'0' => { name: 'William Shakespeare', age_of_death: '52' },
:'1' => { name: 'Unattributed Assistant' },
:'2' => { name: %w(injected names)}
}
}
})
permitted = params.permit book: { authors_attributes: [ :name ] }
assert_not_nil permitted[:book][:authors_attributes]['0']
assert_not_nil permitted[:book][:authors_attributes]['1']
assert_empty permitted[:book][:authors_attributes]['2']
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name]
assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['1'][:name]
assert_filtered_out permitted[:book][:authors_attributes]['0'], :age_of_death
end

This should probably be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants