This repository has been archived by the owner. It is now read-only.

Modify strong_parameters to be more compliant with accepts_nested_attributes_for #87

wants to merge 2 commits into


None yet
2 participants

This is a follow up to #86

I wanted to make strong_parameters work with accepts_nested_attributes_for when fields_for was not the method used to generate fields. In the previous discussion there was some argument about having to dynamically define the permitted fields. However, that is not necessary because the keys do not matter, they do not mean anything, accepts_nested_attributes_for ignores them. In fact, the current strong_parameters code takes this into account by looking for any hash with all numeric keys, assuming that hash is going to be used with accepts_nested_attributes_for, and throwing out the keys.

So, I spent a couple hours learning how strong_parameters works and realized that my previous solution was not particularly good (read pretty bad). It didn't cause any problems with my code, but it did cause some with the gem's tests. Anyway, I went back and did some thinking. I realized that what we really need to look for isn't a regex on every value in the hash, but the name of the hash. If its name ends in _attributes then it should be treated as an attributes hash where the keys are ignored and the values are processed.

This pull request addresses changes the each_element method to do a single regex check on the original key name from hash_filter method. I also tossed in a couple of happy pass tests to make sure that strong_parameters was processing _attributes hashes with random keys and no keys (which are actually arrays not hashes) in addition to the numeric keys fields_for generates.

HurricaneJames added some commits Jan 22, 2013

Changed the assumption that accepts_nested_attributes_for will only b…
…e used with fields_for and the arbitrary decimal number keys. Instead, the code now looks to see if the key ends in _attributes, and deals with such hashes in a more acceptes_nested_attributes_for compliant way. Added a test to check that fields_attributes with random keys are processed properly.

Hi, @HurricaneJames

I've created the gist probably it could help you to deal with the none numeric keys in the nested attributes.


Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.