Reputation: 2278
I receive this error with my contact form in rails:
First argument in form cannot contain nil or be empty
View:
<%= form_for @contact do |f| %>
and so on.......
Controller
def new
@contact = Contact.new
end
and so on....
I found related errors, but none of the solutions seems to fit my particular issue. Any clues to what could be causing this?
Upvotes: 45
Views: 98172
Reputation: 29
I had the same problem. I have a Comment model, a CommentsController, a 'show' action and a 'show' view for the comment. I also have a '_show' partial rendered inside the 'show' view with following local:
:locals => {:comment => @comment}
and inside the '_show' partial I have a
<%= form_for(comment, :remote => true) do |f| %>
which troughs the "First argument in form cannot contain nil or be empty". I only could solve the problem by renaming the local:
:locals => {:new_comment => @comment}
and within the partial:
<%= form_for(new_comment, :remote => true) do |f| %>
It seems using the same name for the Model and the partial local caused some conflict for me.
Upvotes: 0
Reputation: 11
I had the same problem but it was easy to repair. In your #example posts_controler.rb
add the same line to find post like in edit. In my application it's
def edit
@post = Post.find(params[:id])
end
It doesn't work here becase I added it but did not save my changes ;).
Upvotes: 1
Reputation: 175
It can depend on where your new or create action is defined. I had the same issue, turned out i had to put the @Object = object.new command inside the page method.
For example..
class PagesController < ApplicationController
def home
@applicant = Applicant.new
end
def about
end
def homeowner
end
end
I before defined a "new" and "create" method and had the action in them which is usual and i do in other instances. Still don't know why.
Upvotes: 0
Reputation: 2264
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.
Upvotes: 6
Reputation: 3556
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>
Upvotes: 6
Reputation: 31
If in fact your instance variable is supposed to be nil
, you could use
:contact
instead of @contact
and provide a path.
In your case, the code should be,
<%= form_for( :contact , :html => {:class => "form-horizontal", :role => "form"}, url: login_path) do |form| %>
The login_path
is just an example and can be replaced as seen fit.
Upvotes: 2
Reputation: 17631
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
.
Upvotes: 85