rails 5 ForbiddenAttributesError on bulk operations

110 views Asked by At

i try to bulk operation in my rails controller this is my script

def update_by_user
    user_skill_selected = UserSkillSelected.create(params[:user_skill_selected][:users])

    # check through array if all is valid
    if user_skill_selected.all? {|item| item.valid?}
      render json: {json_status: save_success}
    else
      render json: {json_status: save_failed}
    end
end

and this is my user_skill_selected_params

def user_skill_selected_params
    params.require(:user_skill_selected).permit(:user_id, :subskill_id, :skill_id, :users => [])
  end

unfortunately i get an error in my log, the log said

"exception": "#<ActiveModel::ForbiddenAttributesError:ActiveModel::ForbiddenAttributesError>",

after that i try to bulk operations from rails console with using create method with the array value and its work

can anyone solve this... :( sorry for the bad english

1

There are 1 answers

2
brainbag On BEST ANSWER

This can be confusing. Your code is passing in params[:user_skill_selected][:users] to the model create method, instead of your user_skill_selected_params strong parameters, which is why you're seeing that error.

Change this line:

user_skill_selected = UserSkillSelected.create(params[:user_skill_selected][:users])

To this:

user_skill_selected = UserSkillSelected.create(user_skill_selected_params)

And it should eliminate this error.