【问题标题】:NoMethodError for nil:NilClass on RailsNoMethodError for nil:NilClass on Rails
【发布时间】:2019-02-03 13:18:31
【问题描述】:

我有一个名为 Categories 的模型,我想展示它。因此我在 pages/home.html.erb

中输入了以下语句
<%= render 'categories/index.html.erb' %>

现在每当我运行服务器时,我都会得到一个 NoMethodError

<% @categories.each do |category| %>

这是类别视图的完整 index.html.erb 文件:

<p id="notice"><%= notice %></p>

<h1>Categories</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Price</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.title %></td>
        <td><%= category.price %></td>
        <td><%= link_to 'Show', category %></td>
        <td><%= link_to 'Edit', edit_category_path(category) %></td>
        <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_category_path %>
 <p id="notice"><%= notice %></p>

<h1>Categories</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Price</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.title %></td>
        <td><%= category.price %></td>
        <td><%= link_to 'Show', category %></td>
        <td><%= link_to 'Edit', edit_category_path(category) %></td>
        <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_category_path %>
<p id="notice"><%= notice %></p>

<h1>Categories</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Price</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.title %></td>
        <td><%= category.price %></td>
        <td><%= link_to 'Show', category %></td>
        <td><%= link_to 'Edit', edit_category_path(category) %></td>
        <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_category_path %>

有人可以帮我理解为什么这不起作用吗?

【问题讨论】:

  • @spickermann 我不这么认为!我怎么能这样做?

标签: ruby-on-rails ruby model-view-controller


【解决方案1】:

当您也想在您的主页上呈现 categories/index.html.erb 部分时,您必须初始化该部分中使用的所有变量。

因为您在该部分中使用@categories,您需要将以下内容添加到您的控制器操作中。我假设你已经有一个 PagesController 和一个 home 方法。

# in app/controllers/pages_controller.rb
def home
  @categories = Category.all  # <= Add this line
end

【讨论】:

  • 非常感谢您的回答!现在我收到一条错误消息,说“ 行的 #:0x0000000007d1cbb0> 的未定义方法 `category_path'”,你呢?明白我该如何解决这个问题?我急需
  • 我会尝试用link_to 'Show', category_path(category) 替换link_to 'Show', category。如果这不起作用,我建议提出一个新问题,因为这个问题与原始问题无关。您可能希望将您的 config/routes.rb 添加到新问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 2017-07-04
  • 2015-03-19
相关资源
最近更新 更多