why am I getting a ForbiddenAttributesError when u

2019-08-03 08:17发布

问题:

We just upgraded to rails 4.1.13 from 4.0.1. We've dragged our feet at implementing strong parameters. As such, we are still using the protected_attributes gem so we can use the old behavior.

Now, we are getting an ActiveModel::ForbiddenAttributesError. I do not understand why, since we are using the protected_attributes gem..

回答1:

You can basically use the following code snippet to mark attributes safe for mass assignment other than the id attribute of your model

attr_protected :id

Please note that doing so makes all the attributes other than id of your model safe for mass assignment. If most of the attributes of your model can be mass assigned, I think using attr_protected instead of using attr_accessible makes more sense as it does the same job without writing more lines of code.