Admin#index 中没有方法错误

发布于 2025-01-04 06:45:55 字数 1587 浏览 1 评论 0原文

我是一个新手,对使用 Rails 2nd 进行敏捷 Web 开发有疑问。 Ruby 版本 1.8.6。当这本书指示我将脚手架产品放入 admin_controller.rb 时,问题就开始了。我删除了该行,现在收到以下错误消息。

Admin#index 中没有方法错误 显示 admin/index.html.erb 第 10 行引发的位置:

当您没有预料到时,您有一个 nil 对象! 您可能期望一个 Array 的实例。 评估 nil.each 时发生错误

提取的源(大约第 10 行):

7:     <th>Image url</th>
8:   </tr>
9: 
10: <% for product in @product %>
11:   <tr>
12:     <td><%=h product.title %></td>
13:     <td><%=h product.description %></td>

RAILS_ROOT:C:/InstantRails-2.0-win/rails_apps/depot

这是控制器信息:admin_controller 类 AdminController <应用控制器 这里

是视图信息:Views\admin\index.html.erb

列出产品

<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
  </tr>

<% for product in @product %>
  <tr>
     <td><%=h product.title %></td>
    <td><%=h product.description %></td>
    <td><%=h product.image_url %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method          => :delete %></td>
      </tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>

以下是型号信息:Models\product.rb 类产品< ActiveRecord::基础 有

什么建议吗?

I'm a newbie having an issue with Agile Web Development with Rails 2nd. Ruby verison 1.8.6. The issue started when the book instructed me to put scaffold product in the admin_controller.rb. I removed the line and now i'm getting the following error message.

NoMethodError in Admin#index
Showing admin/index.html.erb where line #10 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #10):

7:     <th>Image url</th>
8:   </tr>
9: 
10: <% for product in @product %>
11:   <tr>
12:     <td><%=h product.title %></td>
13:     <td><%=h product.description %></td>

RAILS_ROOT: C:/InstantRails-2.0-win/rails_apps/depot

Here's the controller information:admin_controller
class AdminController < ApplicationController
end

Here's the view information: Views\admin\index.html.erb

Listing products

<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
  </tr>

<% for product in @product %>
  <tr>
     <td><%=h product.title %></td>
    <td><%=h product.description %></td>
    <td><%=h product.image_url %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method          => :delete %></td>
      </tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>

Here's the models information: Models\product.rb
class Product < ActiveRecord::Base
end

Any advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2025-01-11 06:45:55

听起来好像您的控制器中没有设置@products,并且视图中也可能存在拼写错误:

在views\admin\index.html.erb中,将:更改

 <% for product in @product %>  

为:

 <% for product in @products %>

并确保您的控制器具有:

admin_controller.rb

class AdminController < ApplicationController 

  def index
    @products = Product.all
  end

end

Sounds like @products isn't being set in your controller and you also have a possible typo in the view:

in views\admin\index.html.erb, change:

 <% for product in @product %>  

to:

 <% for product in @products %>

and ensure your controller has:

admin_controller.rb

class AdminController < ApplicationController 

  def index
    @products = Product.all
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文