Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Support for nested fields_for with alphanumeric keys #40

wants to merge 1 commit into from

2 participants



I was trying to get the nested_form gem working with strong_parameters. And I am having some trouble getting the two of them working nicely together.

Nested_form is using javascript for adding more or less nested fields for, when working with a model through a form. It is auto creating a unique index key for each set of fields, resulting in nested_attributes parameters with alphanumeric keys, instead of the normal integer based 0,1,2 etc.

I am not quite sure how to fix this, but i have added a failing test describing the problem. I could also try to change nested_form, but I think the solution would be more flexible if strong_parameters supported this.

One solution could be allowing the regexp, currently used for detecting integer based keys, to be changed in a configuration setting.

// Sune


I'm struggling with nested resources as well. However, your problem should IMHO be fixed on the nested_form gem. The prefix author_in keys such as author_1 inside the authors_attributes is redundant and inconsistent with out of the box nested forms. I'm not sure to what extend numeric keys may be a convention already, but it seems to make sense to stick to such a rule in order to keep other code working with the params hash (such as strong_parameters) as configuration free as possible.


Thank you for your comments. I looked into the nested form gem, trying to understand why they prefix some with new_. It is to prevent clashes with the ids generated and existing ids in the db


@kibs kibs referenced this pull request in ryanb/nested_form

Strong_parameters support #199


@kibs: Ah, good point, forgot about that.


nested_form has removed the new prefix, the id is not taken from the key anyway. At least not anymore. I am closing this.

@kibs kibs closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 0 deletions.
  1. +18 −0 test/nested_parameters_test.rb
18 test/nested_parameters_test.rb
@@ -128,4 +128,22 @@ class NestedParametersTest < ActiveSupport::TestCase
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['-1'][:name]
assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-2'][:name]
+ test "fields_for_style_nested_params with alphanumeric keys" do
+ params ={
+ book: {
+ authors_attributes: {
+ :'author_1' => {name: 'William Shakespeare', age_of_death: '52'},
+ :'author_2' => {name: 'Unattributed Assistant'}
+ }
+ }
+ })
+ permitted = params.permit book: {authors_attributes: [:name]}
+ assert_not_nil permitted[:book][:authors_attributes]['author_1']
+ assert_not_nil permitted[:book][:authors_attributes]['author_2']
+ assert_nil permitted[:book][:authors_attributes]['author_1'][:age_of_death]
+ assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['author_1'][:name]
+ assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['author_2'][:name]
+ end
Something went wrong with that request. Please try again.