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
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 %>
3 Comments
Julian Maicher
Don't forget to build an empty phone object in your controller:
@user.phone.buildGSP
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....
ju_293847293
can you use autobuild: true in the model relation to avoid this?