NoMethodError (undefined method `permit' for #<Array:0x007f51c020bd18>
Asked Answered
H

2

17

I am getting this error and using Rails 5.

NoMethodError (undefined method permit' for #<Array:0x007f51cf4dc948> app/controllers/traumas_controller.rb:99:intrauma_params' app/controllers/traumas_controller.rb:25:in `create_multiple'

Controller params are as below.

Started POST "/traumas/create_multiple" for 127.0.0.1 at 2016-10-04 20:09:36 +0530 Processing by TraumasController#create_multiple as JS
Parameters: {"utf8"=>"✓", "fields"=>[{"contusions"=>"1", "burns"=>"", "at_scene"=>"At Scene", "emergency_detail_id"=>"96", "trauma_region"=>"Head-Back"}], "commit"=>"Submit"}

I am trying to create record as below in controller:

  def create_multiple
    trauma_params
    params[:fields].each do |values|
       u = Trauma.create(values)
    end
  end

def trauma_params
        params.require(:fields).permit(:fields => [])
end

Please help me to resolve this issue.

Thanks in advance.

Kiran.

Hardback answered 4/10, 2016 at 14:58 Comment(1)
Do I understand right, that you intend to accept any array including the empty array for the :fields parameter? I believe that the .permit is not the right method to perform input validation. What happens, if you remove the .permit statement?Trawick
P
14

Parameters:

{"fields"=>[{"contusions"=>"1", "burns"=>"", "at_scene"=>"At Scene", "emergency_detail_id"=>"96", "trauma_region"=>"Head-Back"}]}

Safelist array of objects, with the "fields" attribute required:

def trauma_params
  params.permit(fields: [
   :contusions,
   :burns,
   :at_scene,
   :emergency_detail_id,
   :trauma_region
  ])
  .require(:fields)
end

Source: https://edgeguides.rubyonrails.org/action_controller_overview.html#nested-parameters

Papain answered 15/2, 2019 at 0:51 Comment(0)
H
8

I resolved it by referring this (https://github.com/rails/strong_parameters/issues/140) github issue.

EDIT

For parameters like the following (which are in Array):

Parameters: {"fields"=>[{"contusions"=>"1", "burns"=>"", "at_scene"=>"At Scene", "emergency_detail_id"=>"96", "trauma_region"=>"Head-Back"}], "commit"=>"Submit"}

We can do:

def trauma_params
  params.require(:fields).map do |p|
    ActionController::Parameters.new(p).permit(
      :contusions,
      :burns,
      :at_scene,
      :emergency_detail_id,
      :trauma_region
    )
  end
end
Hardback answered 6/10, 2016 at 5:55 Comment(4)
I get NoMethodError (undefined method 'with_indifferent_access' for #<ActionController::Parameters:0x007f996827fac0>):' How do I solve this? Zacek
@JosephN. Rails 5 now returns ActionController::Parameters so there is no need for that call. The third line would now look like: p.permit(.Snakebird
@Snakebird Now it is returning following error ArgumentError (When assigning attributes, you must pass a hash as an argument.):Muleteer
This is the code params.require(:newParams).map do |p| p.permit( :value, :tag, :position ) end newParams is an arrayMuleteer

© 2022 - 2024 — McMap. All rights reserved.