I receive this error with my contact form in rails:
First argument in form cannot contain nil or be empty
View:
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
.
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.
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.
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.
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.
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 ;).