Nested attributes not saving on create

Hi there-

I am a Ruby on Rails beginner trying to work with 2 models, of the
relationship:
[user] has_many [user_details]

I would like to configure my app so that on the create user form, there
are certain default user_details that are created at the same time. I
attempted to perform this by building user_details in the new user
action in the user controller, and then displaying text fields for each
user_detail in the new user form. However, for some reason it seems that
the user_details are not being saved when the form is submitted. The
form redirects to the show user page, and no user_detail fields are
shown.

Does anyone have any idea what the problem might be?

Thank you so much in advance. Any help is appreciated!

On 27 May 2014 11:26, Alessandra P. [email protected] wrote:

user_detail in the new user form. However, for some reason it seems that
the user_details are not being saved when the form is submitted. The
form redirects to the show user page, and no user_detail fields are
shown.

The first thing to do is to work out which bit of code is failing.
First look in log/development.log where you can see the action
requests, sql details and so on. If the request looks ok then you can
debug your code to find out what is going on. Ideally one should use
a debugger such as pry, but one simple way is to insert puts
statements in the code showing the values of data and so on, These
will be printed in the server terminal window. You have to be a
little careful here as, due to buffering, the print may seem to be
slightly out of sync with the surrounding server data.

I presume that you have already worked right through a tutorial such
as railstutorial.org (which is free to use online), which will show
you the basics of rails.

Colin

Hi Colin,

Thanks so much for your reply. I did complete the Hartl tutorial, but
it’s when I tried to incorporate these nested attributes (which the
tutorial does not include) that I started to have problems.

Looking through the log, I see that the post request includes

Unpermitted parameters: user_details_attributes

I suspect that I have to put that user_params in the user controller. I
also know, form the log, that user_details_attributes is a hash. My
guess to make the entries in the hash save to the user, I have to edit
the create action in the user controller to somehow iterate over the
user_details_attributes hash and save each user_detail individually.
Does that sound like I’m on the right track?

On 27 May 2014 12:10, Alessandra P. [email protected] wrote:

Hi Colin,

Thanks so much for your reply. I did complete the Hartl tutorial, but
it’s when I tried to incorporate these nested attributes (which the
tutorial does not include) that I started to have problems.

Looking through the log, I see that the post request includes

Unpermitted parameters: user_details_attributes

It is always worth trying google when you get a message you don’t
understand. Googling for
nested attributes unpermitted parameters
would likely have pointed you at the fact that you have to explicitly
permit attributes that you are posting. Have a look at
http://www.sitepoint.com/rails-4-quick-look-strong-parameters/ which
should get you going.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs