LucyHills

Joined

10 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in How to create new contacts from nested attributes form

In the signin_params method, you need to use contacts_attributes instead of contacts. Here's the corrected code:
private
def signin_params
params.require(:signin).permit(contacts_attributes: [:name, :email, :phone])
end

In the form, you need to use fields_for with the correct association. Since your Signin model belongs_to :contact, you should use fields_for :contact instead of :contacts. Here's the corrected code:
<%= form_with model: [@open_house, @open_house.signins.build], local: true do |f| %>
<% if @signin.errors.any? %>
<% @signin.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
<% end %>

<%= f.fields_for :contact do |c| %>
<%= c.label :name %>
<%= c.text_field :name, placeholder: "Your name", class: "form-control" %>

<%= c.label :email %>
<%= c.text_field :email, placeholder: "Your email", class: "form-control" %>

<%= c.label :phone %>
<%= c.text_field :phone, placeholder: "Your phone", class: "form-control" %>

<% end %>

<%= f.submit 'Sign In', class: "btn btn-primary" %>
<% end %>