First argument in form cannot contain nil or be empty - Rails 4

The error message is telling you that you can't have the following:

<%= form_for nil do |f| %>
<%= form_for [] do |f| %>

My guess here is that your @contact is set to nil and that it doesn't come from your Contact#new action.

FYI it would simply work if you do this:

<%= form_for Contact.new do |f| %>

Though it is not recommended.

You need to check that the view containing your form is actually rendered by the new action of your ContactsController.


I had a similar problem where I had a form in a modal that made changes to a model that was different than what the background page was showing. Even though I had the required @contact = Contact.new in my Contact controller, I needed to add this code within the controller for the background page.

For example, I have a PhoneBook model and within my index view for PhoneBook I have a modal with a form that adds a contact to my Contact model. I need @contact = Contact.new within my index method in my PhoneBook controller:

class PhoneBook < ApplicationController

  def index
    @contact = Contact.new
    ...
  end
end

And then the form within my modal that is being shown on PhoneBook index.html has the same syntax as others have written:

<div id="phoneBook">
  <div id="myModal" class="modal">
    <%= form_for @contact do |f| %>
    ...
  </div>
</div>

I know this has an accepted answer, but that didn't solved my same issue(i didn't wanted to use new in my view) and i intend to post what solved mine.

This is always a typo, you can check your routes with $rake routes if routes are ok, than its some kind of type somewhere else.

In my case i was using sublime editor, and i had an extra def above the def new in my controller, i don't know how it got there, but it was there...Removing it solved my issue after about half hour of nail bitting... There can be any kind of typo causing this issue. This error just means that the new method is not being accessed, if it was being accessed it would have initialized the variable.

I repeat, always a typo somewhere.

Hope this helps anyone.