Ruby-on-rails rails 未定义方法 `each' for nil:NilClass

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13015769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:54:43  来源:igfitidea点击:

rails undefined method `each' for nil:NilClass

ruby-on-rails

提问by Jonathan Hwa

I'm following the tutorial found here. It's simple, and I've followed the instructions exactly to step 6.7. At this point, I get the error

我正在按照此处找到的教程进行操作。这很简单,我完全按照说明进行了步骤 6.7。此时,我收到错误

undefined method `each' for nil:NilClass 

when I try to access index.html.erb on the rails server.

当我尝试访问 rails 服务器上的 index.html.erb 时。

I know the database is working fine, because I can do everything mentioned in step 6.3, create new posts and show/edit/destroy them with absolutely no problems.

我知道数据库运行良好,因为我可以执行步骤 6.3 中提到的所有操作,创建新帖子并显示/编辑/销毁它们绝对没有问题。

Specifically, the issue is with the line

具体来说,问题出在线路上

<% @posts.each do |post| %>

and it's essentially claiming that @postsis nil.

它基本上声称它@posts为零。

I appreciate any help for this ROR newbie! Thanks.

感谢您对这个 ROR 新手的任何帮助!谢谢。

index.html.erb

index.html.erb

<h1>Hello, Rails!</h1>
<table>
  <tr>
    <th>Name</th>
    <th>Title</th>
    <th>Content</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @posts.each do |post| %>
  <tr>
    <td><%= post.name %></td>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
                                     :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to "My Blog", posts_path %>

posts_controller.rb

post_controller.rb

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

采纳答案by Lucas Nogueira

In order for the viewsto work fine in Rails they must be inside the correct directory. This is one of the many implementations of the so called "Convention over Configuration" that Rails loves.

为了views在 Rails 中正常工作,它们必须位于正确的目录中。这是Rails 喜欢的所谓“约定优于配置”的众多实现之一。

So, if you have a method indexand this method is inside a controller named PostsController, you must have a view named indexinside the directory views/posts/. This way, Rails will know that it have to render this view when a get to this method is processed.

因此,如果您有一个方法index并且该方法位于名为 的控制器内PostsController,则您必须index在目录内有一个名为 的视图views/posts/。这样,Rails 就会知道它必须在处理此方法的 get 时呈现此视图。

About a good tutorial, I would recommend this one. It is extense and covers a lot of things that are not just related to Rails itself, like deploying on Heroku and a little CSS.

关于一个很好的教程,我会推荐这一个。它是扩展性的,涵盖了很多与 Rails 本身无关的内容,比如在 Heroku 上部署和一些 CSS。