Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 strong params: make them conditional?

Is there a way to make strong_params conditional? Without the need to write 2 separate methods? In case where one would like to add certain attributes to the permit list when a certain condition is true

For example:

devise_parameter_sanitizer.for(:user) {|u| u.permit(:user,
                                                    :email,
                                                    :role,
                                                    )}

I have this :role attribute permitted in above example. I only want this attribute to be permitted when in Rails.env.development is there a way to do this?

like image 962
Rubytastic Avatar asked Oct 23 '13 08:10

Rubytastic


2 Answers

Does this achieve the desired results?

user_params = [ :user, :email, (:role if Rails.env.development?) ].compact
devise_parameter_sanitizer.for(:user) { |u| u.permit(*user_params) }
like image 68
Frank Joseph Mattia Avatar answered Nov 03 '22 12:11

Frank Joseph Mattia


Haven't found a solution, so I made 2 methods and call the correct param method to handle the records.

like image 44
Rubytastic Avatar answered Nov 03 '22 13:11

Rubytastic