1

How to create form for new User model with embedded Phone model? I've found solution for creating form to add Phone for existing User but how to do that at the same time i create new User?

1 Answer 1

1

You have to create a nested form

<%= form_for @user, :url => users_path do |f| %>

  <%= f.label :name, "Name:" %> <br />
  <%= f.text_field :name %>

  <%= f.fields_for :phone do |p| %>        
    <%= p.label :number, "Phone Number" %> <br />
    <%= p.text_field :number %>
  <% end %>

<% end %>
Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget to build an empty phone object in your controller: @user.phone.build
To get the values to display in an edit field (rails 3.2) I had to do <%= f.fields_for @user.phone do |p| %> rather than simply using the symbol. Not sure if that's specific to my application setup or a common problem....
can you use autobuild: true in the model relation to avoid this?

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.