dynamic list of params to be permited in rails

173 Views Asked by At

I am currently trying to permit params containing an array object.

#My controller
def students_params
  params.require(:student).permit(standards:[], subjects: [])
end

So here in my case, I will have a list of params names to be permitted.

Consider I have a constant which has list as below:

 STUDENTS_PARAMS = ['standands', 'subjects', 'grade', .....]

I tried doing this as below:

#My controller
def students_params
  params.require(:student).permit(*STUDENTS_PARAMS)
end

Problem here is params with array value and getting included. Instead of adding all fields manually is there any other way, I can achieve this?

Thanks you

1

There are 1 best solutions below

0
Haumer On

I take it that you meant to say Problem here is params with array value and ARENT getting included., which is to be expected because its looking for a hash not a symbol/string.

What if you try passing

STUDENTS_PARAMS = [{standands: []}, {subjects: []}, 'grade']

def students_params
  params.require(:student).permit(*STUDENTS_PARAMS)
end