Skip to content

Autocorrect for Rails/StrongParametersExpect fails for accepts_nested_attributes_for params #1429

Open
@chaadow

Description

@chaadow

Expected behavior

I expect this :

    params.require(:company_tool).permit(
      :salary,
      :employees_count,
      rows_prices: {
        row_total_one_employee: [
          :price,
          :cell_id
        ],
        row_total_all_employees: [
          :price,
          :cell_id
        ]
      }

to be autocorrected with :

# Note the double array `[[ ...]]`
    params.expect(
      company_tool: [:salary,
                     :employees_count,
                     rows_prices: [
                        row_total_one_employee: [[ # double array
                         :price,
                         :cell_id
                        ]],
                        row_total_all_employees: [[
                           :price,
                           :cell_id
                        ]]
                     ]
           ]
    )

Actual behavior

it gets autocorrected to this :

    params.expect(
      company_tool: [:salary,
                     :employees_count,
                     {  # unnecessary opening of hash
                        rows_prices: { # Here it's better to open with `[` than `{` according to docs
                           row_total_one_employee: [ # no double array
                              :price,
                              :cell_id
                       ],
                       row_total_all_employees: [ # no double array
                         :price,
                         :cell_id
                       ]
                     }
               }
     ]
    )

which is false, and make row_prices empty when provided with an array

Steps to reproduce the problem

To reproduce you can put the examples above in any controller and you will get the same result

Example can also be found here

RuboCop version

$ [bundle exec] rubocop -V
1.71.0 (using Prism 1.3.0, rubocop-ast 1.37.0, analyzing as Ruby 3.4, running on ruby 3.4.1) [arm64-darwin24]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-graphql 1.5.4
  - rubocop-performance 1.23.1
  - rubocop-rails 2.29.1
  - rubocop-rspec 3.4.0
  - rubocop-rspec_rails 2.30.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions