0

I followed Railscast Episode 165 (revised) to the tee on how to edit and Update multiple records in one form. But when I submit my form to edit multiple records at the same time, I get:

ActiveModel::ForbiddenAttributesError

For this line:

product.update_attributes(params[:product].reject { |k,v| v.blank? })

Params:

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"g5C2siF5GcWfPxhph4utWn8JBs2JXEpIUBDO6OlFyQQ=",
 "product_ids"=>["11142",
 "11143"],
 "product"=>{"user_id"=>"",
 "allow_multi_users"=>"true",
 "state_id"=>"",
 "site"=>"",
 "department"=>"",
 "room"=>"",
 "asset_type_id"=>"",
 "asset_model_id"=>"",
 "sync_with_jss"=>"",
 "carrier_id"=>"",
 "mobile_contract_req_date"=>"",
 "mobile_contract_end_date"=>"",
 "mobile_international_plan"=>"",
 "mobile_tethering"=>"",
 "mobile_account"=>""},
 "commit"=>"Update"}`

Normally, I would think this is because I have not permitted an attribute in strong parameters. But this isn't a attribute, it's the params for the form that hold all the values.

This is for my Products.rb model so shouldn't it already accept params[:product]?

products_controller.rb

private  
  def product_params
    params.require(:product).permit(:mobile_account, :mobile_international_plan, :mobile_tethering, :mobile_contract_end_date.. )
  end

Using Rails 4.0.0

Devin
  • 1,011
  • 2
  • 14
  • 30

1 Answers1

2

Try this

def update
  ...
  product.update_attributes(product_params)
  ...
end

private

  def product_params
    params.require(:product).permit(:mobile_account, :mobile_international_plan, :mobile_tethering, :mobile_contract_end_date.. )
  end

The list of fields are the fields you expect your user send to update or create.

You can find more information here

The error ActiveModel::ForbiddenAttributesError is raised when you try to update a model with a params object with params that don't were permited, all the fields present at the form, must be permited to update the record.

Alejandro Babio
  • 5,189
  • 17
  • 28