820 Experience
0 Lessons Completed
1 Question Solved


Posted in How to create new contacts from nested attributes form

Hey Red, I am running into a new challenge that I thought you would understand well. Picking up the discussion above, now I would like to update my form action to create a new contact (if a contact doesn't exist ... as defined by the email field) or find and update that contact with the provided form details. Through some research, I discovered:


In my join model I now have:

def autosave_associated_records_for_lead
    if new_lead = Lead.find_by_email(
       self.lead = new_lead

The problem I'm running into is that this doesn't update the contact, if the contact exists. Any chance you could help me solve that last problem?

Posted in How do I use phonelib gem for a nested resource?

I am setting up telephone validation for a Lead model. Leads have a field called :phone. I'm using phonelib to validate the contents of that field. My New Lead form is nested within a second model, therefore the second model accepts_nested_attributes_for :leads. Without phonelib, my leads are saved just fine. With phonelib, I'm getting the following error in my :create action:
wrong number of arguments (given 3, expected 1..2)
Strong params are all set, and the model validations are set. Not sure what's hanging this up. Any ideas?

Posted in How to create new contacts from nested attributes form

Thanks Red, I finally found my problem and solved it! You are right, all the inverse_of stuff was unnecessary (I removed). Everything boiled down to my f.fields_for :contact line. I changed that to:

<%= f.fields_for :contact, do |c| %>

...and voila, everything worked! I feel like the Contact instantiation should be in the controller though.. it's just that I can't seem to get the Contact instantiated from the :new action with everything I tried. 🤷🏼‍♂️

Thanks Red, I've added the ID back in (mistakenly pulled it out as I was experimenting). I'll definitely reconsider my :signin model name as well. I am now running into an errors that says: Could not find the inverse association for contact (:signin in Contact). I'm wondering if this comes back to an incorrect Inverse_of declaration (Thoughtbot wrote on it here: But I just can't seem to get it working.

I'm building my first semi "real world" rails app (an Open House management app for real estate). I am currently stuck on a "accepts nested attributes" problem. Here are the details... (I've provided all code below)

An open house can have many contacts (attendees), and a contact can attend many open houses. So, I have a join model called Signin. When someone "signs in" to an open house via the Open House > Sign In form, I would like a "sign in" instance to be created along with a contact via nested attributes. Note: Since this is a "has many through", the "Sign in" and "contact" models are not the traditional "parent/child" relationship.

When attempting to submit my Signin form, the form error says "Contact must exist" and the log reports "Unpermitted parameter: :contacts". (I do have strong parameter sit, but perhaps incorrectly).

At this point, I just feel like I've tried everything, but I can't get the form to create a Contact as well as a Signin instance.

The relevant models:

class OpenHouse < ApplicationRecord
belongs_to :listing
has_many :signins, inverse_of: :open_house
has_many :contacts, through: :signins

class Signin < ApplicationRecord
belongs_to :open_house
belongs_to :contact
accepts_nested_attributes_for :contact

class Contact < ApplicationRecord
has_many :signins
has_many :open_houses, through: :signins

Routes: (Signins are nested within each open house to allow easy sharing of sign in form)
resources :open_houses do
resources :signins
resources :contacts

The Signin Controller:

class SigninsController < ApplicationController
def new
@open_house = OpenHouse.find(params[:open_house_id])
@signin =

def create
@open_house = OpenHouse.find(params[:open_house_id])
@signin =
redirect_to @signin, notice: 'You are now signed in'
render :new

def show
@signin = Signin.find(params[:id])

def signin_params
params.require(:signin).permit( contacts_attributes: [:name, :email, :phone])

Signin Form:

<%= form_with model: [ @open_house, ], local: true do |f| %>
<% if @signin.errors.any? %>

    <% @signin.errors.full_messages.each do |msg| %>
  • <%= msg %>

  • <% end %>

<% end %>
<%= f.fields_for :contacts 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 %>

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.