0

I'm stuck with strong_parameters and this array of hashes with a dynamic hash (hstore) inside.

The structure is the following:

{ contact_sources: [
  { id: 1, filled_fields: { randomstuff: 'randomdata', dunno: 123 } },
  { id: 2, filled_fields: { blah: 'blabla', dunno: 9043 } }
] }

So, my main attempt is the following:

params.permit(contact_sources: [{:filled_fields => []}, 'id'])

Which doesn't return filled_fields. Any suggestion on how to deal with it?

Update 1: I have the following model:

class ContactSource < ActiveRecord::Base
  # Fields: id:integer, filled_fields:hstore
end

In my action, I'm submitting multiple records at once (mass update), so I have an array of contact_source, but actually they don't belong to anything, it's just a mass update.

5
  • I'm not quite understanding how this is set up... but perhaps you could use an attr_accessor to refer to the contact_sources hash and then permit the attr_accessor? Jul 17, 2014 at 4:36
  • @user3334690: No, I don't have any association, see my update 1 it should explain better what I'm doing Jul 17, 2014 at 18:55
  • what does the form look like? Jul 18, 2014 at 4:26
  • No form @user3334690 , I perform an ajax request and I pass that array Jul 18, 2014 at 16:49
  • oh I think I see what you're doing now... I think what you're looking for is merge (and maybe deep_merge given your hash) instead of permit... I'm not that familiar with strong parameters yet, though, so I'm not exactly sure how to use them Jul 18, 2014 at 17:38

1 Answer 1

0

Looks like it's not possible to do it with "plain" strong_parameters syntax. The only option you have is to actually, after filtering, re-add those values with a loop. I know it's terrible but it's the only way right now. I submitted a bug to Rails actually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.