Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `with_indifferent_access' for

In my Rails application I trying to merge some params:

def shared_incident_params
    params.require(:drive_off_incident).permit(:incident_time, :product,
      :amount_cents, :where_from, :where_to, car_attributes: [:brand_id,
      :model, :color, :body_type, :plates], witness_attributes: [:first_name, :last_name, :email, :phone],
      notes_attributes: [:id, :content])
  end

  def drive_off_incident_params
    shared_incident_params.merge(person_description_attributes: [:height,
      :age, :gender, :nationality, :features, :clothes])
  end

But this code gives me the following error:

NoMethodError:
   undefined method `with_indifferent_access' for [:height, :age, :gender, :nationality, :features, :clothes]:Array

any ideas?

like image 810
Mateusz Urbański Avatar asked Dec 09 '25 14:12

Mateusz Urbański


1 Answers

Are you sure you want to merge the return value of shared_incident_params with the hash in drive_off_incident_params? That value is probably a Parameters object, but you're trying to merge a hash into it. Parameters inherits from ActiveSupport::HashWithIndifferentAccess, which tries to coerce the other value into the same type when merging.

I guess that what you're trying to do is to extend the rules in shared_incident_params when running drive_off_incident_params.

Have you tried just doing something like this:

def shared_incident_params
  params.require(:drive_off_incident).permit(*permitted_incident_params)
end

def permitted_incident_params
  [
    :incident_time, 
    :product,
    :amount_cents, 
    :where_from, 
    :where_to, 
    car_attributes: [:brand_id, :model, :color, :body_type, :plates], 
    witness_attributes: [:first_name, :last_name, :email, :phone],
    notes_attributes: [:id, :content]
  ]
end

def drive_off_incident_params
  shared_incident_params
  params.permit(
    person_description_attributes: [
      :height,
      :age, 
      :gender, 
      :nationality, 
      :features, 
      :clothes ]
  )
end
like image 181
Jesper Avatar answered Dec 11 '25 13:12

Jesper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!