0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

An array can be used inside Parameters. github.com/rails/strong_parameters#nested-parameters
I agree for nested attributes, e.g "nested_2" => [], however having such: profile: ["nested_1" => "a descr", "nested_2" => "other desc"] and then params.require(:profile).permit(...) will end with exception.
parameters.permit(profile: []) works fine here.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.