一种布局中有两个控制器,导轨 3
好的,我有两个模型,一个配方模型和一个类别模型。在我的布局(application.html.erb)中,我有一个主容器 div,它“产生”食谱索引操作。我试图通过在无序列表中迭代它们来将所有类别名称列为侧边栏(也是 div)中的链接。当您单击其中一个链接时,它将转到类别显示页面,然后该页面将列出该类别中的所有食谱。
以下是我尝试列出链接的方式 -
<div class="container" id="categories">
<% for category in @categories %>
<ul>
<li><%= link_to category.name, category %></li>
</ul>
<% end %>
</div>
问题是我收到 NoMethodError -
当你没有预料到时,你有一个 nil 对象! 您可能期望一个 Array 的实例。 评估 nil.each 时发生错误
它没有从模型中检索记录。任何关于如何完成这项工作的建议将不胜感激。我尝试像其他一些类似的帖子所说的那样渲染部分内容,但仍然遇到相同的错误。
这是确切的错误 -
NoMethodError in Recipes#index
Showing /Users/grizlord/Rails/recipe2/app/views/layouts/application.html.erb where line #39 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 #39):
36: </div>
37: <div class="container" id="categories">
38: Browse by Category
39: <% for category in @categories %>
40: <ul>
41: <li><%= link_to category.name, category %></li>
42: </ul>
Okay, I have two models, a recipe model and a category model. In my layout(application.html.erb) I have a main container div that "yields" the recipes index action. I'm trying to list all the category names as links in a side bar(also a div) by iterating over them in an unordered list. When you click one of the links it will go to the category show page which will then list all the recipes in that category.
Here is how I'm trying to list the links in -
<div class="container" id="categories">
<% for category in @categories %>
<ul>
<li><%= link_to category.name, category %></li>
</ul>
<% end %>
</div>
The problem is I get a NoMethodError -
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
It is not retrieving the records from the model. Any suggestions on how to get this done would be greatly appreciated. I tried to render a partial as some of the other similar posts have said but still get the same error.
This is the exact error -
NoMethodError in Recipes#index
Showing /Users/grizlord/Rails/recipe2/app/views/layouts/application.html.erb where line #39 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 #39):
36: </div>
37: <div class="container" id="categories">
38: Browse by Category
39: <% for category in @categories %>
40: <ul>
41: <li><%= link_to category.name, category %></li>
42: </ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有从控制器的数据库中获取
@categories
。You are not fetching
@categories
from the database in your controller.