Ruby-on-rails 表单中的第一个参数不能包含 nil 或为空 - Rails 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18853721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
First argument in form cannot contain nil or be empty - Rails 4
提问by nil
I receive this error with my contact form in rails:
我在 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?
我发现了相关错误,但似乎没有一个解决方案适合我的特定问题。任何可能导致这种情况的线索?
回答by Pierre-Louis Gottfrois
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 @contactis set to niland that it doesn't come from your Contact#newaction.
我的猜测是您@contact的设置为nil并且它不是来自您的Contact#new操作。
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 newaction of your ContactsController.
您需要检查包含您的表单的视图实际上是由new您的ContactsController.
回答by quicklikerabbit
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.
我有一个类似的问题,我在模态中有一个表单,该表单对与背景页面显示的不同的模型进行了更改。即使我的 Contact 控制器中有所需的 @contact = Contact.new,我也需要在后台页面的控制器中添加此代码。
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:
例如,我有一个 PhoneBook 模型,在我的 PhoneBook 索引视图中,我有一个带有表单的模式,该表单向我的 Contact 模型添加了一个联系人。我需要 @contact = Contact.new 在我的 PhoneBook 控制器的 index 方法中:
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:
然后在我的模态中显示在 PhoneBook index.html 中的表单具有与其他人编写的相同的语法:
<div id="phoneBook">
<div id="myModal" class="modal">
<%= form_for @contact do |f| %>
...
</div>
</div>
回答by Zia Ul Rehman Mughal
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.
我知道这有一个公认的答案,但这并没有解决我同样的问题(我不想使用 new 在我看来),我打算发布解决了我的问题。
This is always a typo,you can check your routes with $rake routesif routes are ok, than its some kind of type somewhere else.
这总是一个错字,您可以检查您的路线$rake routes是否正确,而不是其他地方的某种类型。
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.
就我而言,我使用的是 sublime 编辑器,并且我的控制器中的 def new 上方有一个额外的 def,我不知道它是如何到达那里的,但它就在那里......大约半小时后删除它解决了我的问题咬指甲......可能有任何类型的错字导致此问题。此错误仅表示未访问新方法,如果正在访问它,它将初始化变量。
I repeat, always a typo somewhere.
我再说一遍,总是在某个地方打错字。
Hope this helps anyone.
希望这可以帮助任何人。
回答by Kafka
If in fact your instance variable is supposed to be nil, you could use
:contactinstead of @contactand provide a path.
如果实际上您的实例变量应该是nil,则可以使用
:contact代替@contact并提供路径。
In your case, the code should be,
在你的情况下,代码应该是,
<%= form_for( :contact , :html => {:class => "form-horizontal", :role => "form"}, url: login_path) do |form| %>
The login_pathis just an example and can be replaced as seen fit.
这login_path只是一个例子,可以根据需要更换。
回答by ?ukasz Zaj?c
I had the same problem but it was easy to repair. In your #example posts_controler.rbadd the same line to find post like in edit. In my application it's
我有同样的问题,但很容易修复。在您#example posts_controler.rb添加同一行以查找帖子中的编辑内容。在我的应用程序中
def edit
@post = Post.find(params[:id])
end
It doesn't work here becase I added it but did not save my changes ;).
它在这里不起作用,因为我添加了它但没有保存我的更改;)。
回答by Mahan Lamei
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:
我有同样的问题。我有一个评论模型、一个 CommentsController、一个“显示”操作和一个评论的“显示”视图。我还在“show”视图中渲染了一个“_show”部分,其中包含以下本地内容:
:locals => {:comment => @comment}
and inside the '_show' partial I have a
在“_show”部分里面我有一个
<%= 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:
它通过“表单中的第一个参数不能包含 nil 或为空”。我只能通过重命名本地来解决问题:
: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.
似乎对模型使用相同的名称,部分本地对我造成了一些冲突。
回答by DanTheMann
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.
这可能取决于您的 new 或 create 操作的定义位置。我遇到了同样的问题,结果我不得不将 @Object = object.new 命令放在 page 方法中。
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.
我之前定义了一个“新”和“创建”方法,并在其中执行了通常的操作,我在其他情况下也会这样做。还是不知道为什么。

