可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
.
回答2:
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.
回答3:
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>
回答4:
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.
回答5:
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.
回答6:
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 ;).