2

How to permit this parameters:

contacts: [ 
    {:value => 'value', :contacts_type => 'contact_type'}, 
    {:value => 'value', :contacts_type => 'contact_type'}, 
]

To create many objects by controller action in one JSON request?

xdazz
  • 158,678
  • 38
  • 247
  • 274
kuatro
  • 481
  • 1
  • 5
  • 17

3 Answers3

3

Like below, contacts will be an array of resources with specific attributes value and contacts_type:

params.permit(contacts: [:value, :contacts_type])
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • So how 'create' method should look like? If I just using `company.contacts.create(contacts_params)` it returns error: `ActiveRecord::UnknownAttributeError (unknown attribute: contacts)` – kuatro Apr 09 '14 at 05:48
  • @kuatro add `attr_accessor :contacts` to your model class. It shall work. – Alok Anand Apr 09 '14 at 05:56
  • @AlokAnand adding attr_accessor :contacts will create the record with nil values – error-404 Sep 18 '17 at 12:27
0

If you get params like the following:--

:params=>{:xyz => {:contacts => [{:value => 'value', :contacts_type => 'type'}, ..]}}

Then do the folowing:--

params.require(:xyz).permit(contacts: [:value, :contacts_type])

And add attr_accessor :contacts to your model if contacts is just a form field name part.

Alok Anand
  • 3,346
  • 1
  • 20
  • 17
0

Work around for this should be

def contact_params
   new_params = params.permit(contacts: [:value, :contacts_type])
   new_params[:contacts] if new_params
end

Please suggest alternate solution if any

error-404
  • 229
  • 4
  • 18