Allowing only certain values though a strong parameter in Rails 4

Viewed 6537

I have a field otp_set_up, which in the company_user model is allowed to be "true" or "false".

There is a use case where a sys admin user can reset this field to "false".

While the field can be set to "true" through code, NO user can set it to "true" via a form edit etc.

I haven't added to it the validation in the model since it can be "true" or "false".

I have the following code in a params method specific to an update in the controller before the params.require .permit bit:

if curr_company_user.is_sys_admin? && curr_company_user.can_crud_company_users? && params[:id].to_i != curr_company_user.id

  params[:company_user] = params[:company_user].except(:otp_set_up) if params[:company_user][:otp_set_up] == true
  params.require(:company_user).permit(:otp_setup, etc. etc....

elsif etc. etc...

This works. A Sys admin user can not set otp_set_up to "true".

My question is:

Is this the best and correct way to do this in Rails? It seems a bit hacky to me, going through the params hash and removing a bit.

Is there a better / cleaner way?

3 Answers
Related