Admin#index 中没有方法错误
我是一个新手,对使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来好像您的控制器中没有设置@products,并且视图中也可能存在拼写错误:
在views\admin\index.html.erb中,将:更改
为:
并确保您的控制器具有:
admin_controller.rb
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:
to:
and ensure your controller has:
admin_controller.rb