Ruby-on-rails Form_for“表单中的第一个参数不能包含 nil 或为空”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15379495/
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
Form_for "First argument in form cannot contain nil or be empty" error
提问by Greg
I cannot figure out why I'm getting this error, and exactly what it means.
我无法弄清楚为什么我会收到此错误,以及它的确切含义。
First argument in form cannot contain nil or be empty(Line 3)
表单中的第一个参数不能包含 nil 或为空(第 3 行)
Add a new Post
添加新帖子
<%= form_for @post do |f| %> //Error here
<p>
<%= f.label :title, 'Title' %><br/>
<%= f.text_field :title %><br/>
</p>
<p>
<%= f.label :content, 'Content'%><br/>
<%= f.text_area :content %><br/>
</p>
<p>
<%= f.submit "Add a New Post" %>
</p>
<% end %>
Controller:
控制器:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = post.new(params[:post])
if @post.save
redirect_to posts_path, :notice => "Your post was saved"
else
render "new"
end
end
def edit
end
def update
end
def destroy
end
end
回答by Jim Stewart
Assuming you are rendering this from PostsControllerand using the conventional view name, your newmethod should create a new Postand assign it:
假设您从PostsController传统视图名称渲染它并使用它,您的new方法应该创建一个新的Post并分配它:
def new
@post = Post.new
end
You can use the class name (as @Yuriy suggested), but the conventional way is to instantiate a new object. That allows you to re-use the same form view for rendering errors after a save.
您可以使用类名(如@Yuriy 建议的那样),但传统的方法是实例化一个新对象。这允许您在保存后重新使用相同的表单视图来呈现错误。
If you want to see how this normally looks, create a new Rails project and use the scaffold generator to create some sample code.
如果您想查看它通常的外观,请创建一个新的 Rails 项目并使用脚手架生成器创建一些示例代码。
回答by Tarique
Use this (not recommended). It should work.
使用这个(不推荐)。它应该工作。
<%= form_for Post.new do |f| %>
回答by Clint Feekes
I was getting the same error message. I was just trying to get simpleform up and working and created a subdirectory within my view for testing forms when I got this error message and thought that simpleform wasn't installed.
我收到了同样的错误信息。当我收到此错误消息并认为未安装 simpleform 时,我只是想启动并运行 simpleform,并在我的视图中创建了一个子目录用于测试表单。
I assumed that instance variables like @userwere available to all my view files including those in lets say the @clientssubdirectory and the clients/_formused for creating new clients. My assumption was incorrect as @userwill evaluate to Nilwhen used in say the clients/_formwhen it's created in the @userController.
我假设像这样的实例变量@user可用于我的所有视图文件,包括可以说@clients子目录和clients/_form用于创建新客户端的那些。我的假设是不正确的,因为在使用时@user会评估它在控制器中创建的时间。 Nilclients/_form@user
回答by Yuri Golobokov
Your @postis nil
你的@post是nil
You should form_for Postinstead for new post.
你应该form_for Post改为新职位。
回答by Prabhakar Undurthi
To understand this error there are two specific reasons why you get this error.
要了解此错误,出现此错误的具体原因有两个。
when you try to access the instance variable which is not defined or misspelled. For example in your controller if you define like
def new @post = Post.new end
当您尝试访问未定义或拼写错误的实例变量时。例如在你的控制器中,如果你定义像
def new @post = Post.new end
and you try to access in the view like
并且您尝试在视图中访问
<%= form_for @posts method: :post do |f| %> #plural name
in the above case you get the same error.
在上述情况下,您会遇到相同的错误。
Another case is if you don't define the instance or don't assign the instance variable in your controller and try to access it in the view you'll get the same error.
另一种情况是,如果您未定义实例或未在控制器中分配实例变量并尝试在视图中访问它,您将收到相同的错误。

