一种布局中有两个控制器,导轨 3

发布于 2024-10-21 06:06:17 字数 1240 浏览 1 评论 0原文

好的,我有两个模型,一个配方模型和一个类别模型。在我的布局(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 技术交流群。

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

发布评论

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

评论(1

记忆消瘦 2024-10-28 06:06:17

您没有从控制器的数据库中获取@categories

You are not fetching @categories from the database in your controller.

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