4

I'm trying to build a multi model form - but one of the issues, is I need to link the models within the form.

For example, lets say the form I have has the following models: Users, Profiles

When creating a new user, I'd like to create a new profile simultaneously, and then link the two. The issue is, if neither have been created yet, they don't have ID's yet - so how can I assign linking values?

Thanks!

-Elliot

I noticed, some people are favoriting this - to view more about linking the two models, check out my second question which has the answer: Linking two models in a multi-model form

1 Answer 1

5

One way to achieve the desired result is that you can craft a form that takes advantage of nested attributes support in Rails:

<%= form_for(@user) do |f| %>
  <%= f.label :my_user_attribute %>
  <%= f.text_field :my_user_attribute %>

  <%= f.fields_for :profile do |fp| %>
    <p>
      <%= fp.label :my_profile_attribute %>
      <%= fp.text_field :my_profile_attribute %>
    </p>
  <% end %>    

  <%= f.submit %>
<% end %>

You will also need to add the following to your User class:

accepts_nested_attributes_for :profile

You can read more about Active Record Nested Attributes here. You can read more about the ActionView Form Helpers here (search down the page for "Nested Attributes Examples").

If you use this approach, along with good validations on both models, you won't have to worry about tracking the database IDs, because both will be created at the same time by ActiveRecord (but not until both model objects are valid).

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

5 Comments

Hey Scott, thanks for the response! I'm not really sure I understand the code you posted - I tried it out, and I'm getting an undefined variable or method 'fp' error?
Hi Elliot - I've updated my code - I made a mistake. The first couple of form builder variables should be 'f' and not 'fp' (Lines 2 & 3 updated). Apologies - was writing way too fast! Essentially the code allows you to do nested form input fields for parent-child associated objects.
Thanks Scott! I updated it as you said, and the form_for(@users) appears, but the fields_for profile section seems to be hidden when viewing it?
Okay, then you are nearly there. fields_for (like form_for) will only produce form fields for an object that exists (non-Nil). So in your controller (inside your 'new' action) where you create a new User e.g. '@user = User.new', you also need to add a line where you create an empty new profile object on your @user... e.g. '@user.profile = Profile.new'
That's good to hear. One final thing: If at some point you start tightening up security of your app by using 'attr_accessible' in your User model, then you will also need to add :profile_attributes to that 'accessible' list in order for your nested form attributes to be updated on @user.profile (as they are now). If this doesn't make sense, then hopefully it will point you in the right direction when you need it (Something to Google for) :-)

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.