Ruby-on-rails nil:NilClass 的未定义方法`each'...为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20774180/
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
undefined method `each' for nil:NilClass... why?
提问by user3134762
rails guide example, click the button save post, console show this message:
rails 指南示例,单击按钮保存帖子,控制台显示此消息:
Started POST "/posts" for 127001 at 2013-12-25 22:42:04 +0800 Processing by PostsController#create as HTML Parameters: {"utf8"=>"?", "authenticity_token"=>"CLalUww3gqnSlED0AWdou6P/U2qya vPqDiBANQOuYgA=", "post"=>{"title"=>"11", "text"=>"22"}, "commit"=>"Save Post"} (0.0ms) begin transaction (0.0ms) rollback transaction Redirected to http:// 127001:3000/posts Completed 302 Found in 16ms (ActiveRecord: 0.0ms)
Started GET "/posts" for 127001 at 2013-12-25 22:42:04 +0800 Processing by PostsController#index as HTML Rendered posts/index.html.erb within layouts/application (15.6ms) Completed 500 Internal Server Error in 31ms
ActionView::Template::Error (undefined method `each' for nil:NilClass):
<th>Text</th> </tr> <% @posts.each do |post| %>======================================================
2013 年 12 月 25 日 22:42:04 +0800 开始为 127001 POST "/posts" 由 PostsController#create 处理为 HTML 参数:{"utf8"=>"?", "authenticity_token"=>"CLalUww3gqnSlED0AWqBouyaOgAugUqY =", "post"=>{"title"=>"11", "text"=>"22"}, "commit"=>"Save Post"} (0.0ms) 开始事务 (0.0ms) 回滚事务重定向到 http://127001:3000/posts Completed 302 Found in 16ms (ActiveRecord: 0.0ms)
在 2013-12-25 22:42:04 +0800 开始 GET "/posts" for 127001 由 PostsController#index 处理为 HTML 在布局/应用程序中渲染posts/index.html.erb (15.6ms) Completed 500 Internal Server Error in 31ms
ActionView::Template::Error(nil:NilClass 的未定义方法 `each'):
<th>Text</th> </tr> <% @posts.each do |post| %>================================================== ====
routes is correct, why post is nil? rails 4.0.2 ruby 2.0
路线是正确的,为什么帖子是零?导轨 4.0.2 红宝石 2.0
回答by vee
In your posts controller, you need to define @posts, which, based on the error you haven't.
在您的帖子控制器中,您需要@posts根据您尚未定义的错误定义, 。
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all
end
end
As @postsis not defined calling eachon it will generate undefined methodeach' for nil:NilClass`.
由于@posts未定义,调用each它将undefined method为 nil:NilClass`生成每个'。
回答by Prabhakar Undurthi
To explain more about this error whenerver you encounter
在您遇到时解释有关此错误的更多信息
undefined method `each' for nil:NilClass
The error clearly complaints that you're calling each method on something here(@posts) which is nil. That means you've not defined it in your controller. Since you didn't define it, that's why it is complaining undefined method for nil class.
该错误清楚地抱怨您在此处(@posts) 上调用每个方法都为零。这意味着您尚未在控制器中定义它。由于您没有定义它,这就是为什么它抱怨 nil 类的未定义方法。
Please makes sure to check whenever you call an instance variable from your view? you must have to define that in your controller to be accessible in views.
请确保在您从视图中调用实例变量时进行检查?您必须在控制器中定义它才能在视图中访问。
Sometimes you'll also get this error if you call a private method in your controller.
有时,如果您在控制器中调用私有方法,您也会收到此错误。

