user3043498
user3043498

Reputation: 3

Rails, Angular, REST API with nested attributes

I'm using Rails as a proxy between a REST server and an AngularJS frontend. The rest resources (POST/GET) are stored as class objects (no ActiveRecord because no local db). I have an issue with nested attributes example {profile: [{"nested_1": "a descr"} , {"nested_2": "other desc"}]}

How do i tell the Rails controller to allow nested attributes for the object sent by Angular ?

I an Unpermitted Parameters error when the controller processes updates/creates actions

P.S. I'm not fluent in Rails and all answers to the unpermitted params / with nested attributes error relates to ActiveRecord which i don't rely on.

Thank you for your help.

Upvotes: 0

Views: 509

Answers (1)

blelump
blelump

Reputation: 3243

Your profile params should be slightly rewritten to match strong parameters policy:

params = {profile: {"nested_1" => "a descr", "nested_2" => "other desc"} }
parameters = ActionController::Parameters.new(params)
parameters.require(:profile).permit(:nested_1, :nested_2)
#  => {"nested_1"=>"a descr", "nested_2"=>"other desc"}

As can be seen here, Parameters class extends HashWithIndifferentAccess so that profile params must not be an Array, but a Hash.

Upvotes: 1

Related Questions