undefined方法"each"rails 4.0

Tru*_*ran 1 each ruby-on-rails erb

我认为我是个错误:

undefined method `each' for nil:NilClass
<tbody>
   <% @invoices.each do |invoice| %>
     <tr>
       <td><%= invoice.invoiceDate%></td>
       <td><%= invoice.invoiceNumber %></td>
Run Code Online (Sandbox Code Playgroud)

这是我的发票控制器索引:

def index
    @invoices = Invoice.all
end
Run Code Online (Sandbox Code Playgroud)

我见过的关于这个错误的大多数帖子是因为@invoices没有正确声明,不像我的.任何人有任何想法还有什么可能是错的?

谢谢!!

Joe*_*edy 5

由于您使用@invoices在你的节目来看,实际上你需要设置@invoices你的show方法,而不是你的index方法.因此,将以下代码添加到您的控制器,您应该全部设置.

def show
  @invoices = Invoice.all
end
Run Code Online (Sandbox Code Playgroud)