首页 > 解决方案 > Ruby on Rails - 未定义的方法“类别”#你的意思?类别 ID

问题描述

错误消息截图 我现在在大学里学习 Ruby on Rails,我正在努力学习它。我有一个项目要在接下来的几天内完成,但我无法找出我的代码有什么问题,我试图修复它,我已经用谷歌搜索了它,但没有运气。

由于冠状病毒,教育机构关闭,我无法向我的讲师寻求帮助。

如果有人可以帮助我解决这个问题,我将不胜感激?

我的应用程序非常简单,人们可以在其中发布他们想要的任何内容,但是,他们需要选择一个类别并且其他人可以对其发表评论。

我在浏览器上显示的错误如下:

# 的未定义方法“类别”

你的意思?类别 ID

模板包含的痕迹:#

app/views/layouts/_breadcrumb_nav.html.erb:21 app/views/layout/application.html.erb:16

要求

参数:

{"id"=>"3"}

我显示错误的代码如下:

下面是 /app/views/layouts/_breadcrumb_nav.html.erb

<!-- Check if there is a post present, a category is present and handle these two scenarios differently, also includes a default link back to the home page -->
<div class="inner">

  <!-- Check if the user is signed in, otherwise just show the normal link -->
  <% if account_signed_in? %>
    <%= link_to "Sign Out", destroy_account_session_path, method: :delete, class: "pull-right m-l-10" %>
    <% else %>

  <%= link_to "Account", root_path, class: "pull-right m-l-10" %>
    <% end %>

  <%= link_to "New Post", new_post_path, class: "pull-right" %>

  <%= link_to "Home", root_path %> >>

  <!-- Do not show category if category if it is new entry and has no category set yet
       check out application_helper.rb
  -->

  <% if @post.present? && !@post.new_record?
       category = @post.category
       parent = category.parent if category.present?
  %>


    <% if parent.present? %>
      <%= link_to parent.name, category_path(parent) %> >>
    <% end %>

    <%= link_to category.name, category_path(category) %> >>
    <%= @post.title %>
  <% elsif @category.present?
       parent = @category.parent
  %>
    <% if parent.present? %>
      <%= link_to parent.name, category_path(parent) %> >>
    <%end %>

      <%= @category.name %>
    <% end %>
</div>

下面是 /app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Marketplace</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all' %>
    <%= javascript_pack_tag 'application' %>
  </head>

  <body>


  <div class="header" >
    <%= render "layouts/breadcrumb_nav" %>
  </div>


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


    <!-- divs for the menu -->
    <div class="main">
      <div class="main-left">
        <p><strong><%= link_to "Marketplace", root_path %></strong></p>

        <!-- Hides the Create a New Post menu once the user leaves the home page -->
        <p><%= link_to "Create a New Post", new_post_path %></p>

        <!-- User login - devise gem-->
        <p><%= link_to "User Login", new_account_session_path unless account_signed_in? %></p>

        <!-- load in categories if the user on a category page, only loads if the categories controller is being used -->
        <%= render "categories/category_sidebar"  if controller.controller_name == "categories" %>



        <% if is_home? %>
        <div class="nav-pages">
          <p><%= link_to "faqs, help", faqs_path %></p>
          <p><%= link_to "learn more about marketplace", about_path %></p>
          <p><%= link_to "avoid scams, fraud, safety tips", fraud_path %></p>
          <p><%= link_to "terms of use", terms_path %></p>
        </div>
        <% end %>

      </div>
      <div class="main-right">

    <%= yield %>

      </div>
    </div>

  </body>
</html>

下面是 /app/models/post.rb

class Post < ApplicationRecord
  # Setting up the relationship in the DB
  belongs_to :parent, class_name: "Category", optional: true
  has_many :subcategories, class_name: "Category", foreign_key: :parent_id, dependent: :destroy
  has_many :posts
end

我对 Java 和 Ruby on Rails 有一点了解,这让我很生气,我希望你能帮助我。

非常感谢理查德

标签: ruby-on-railsrubymethodscloudundefined

解决方案


我猜您正在尝试构建类似于面包屑导航的东西,看起来像这样:

Authors >> French >> Albert Camus

首先设置关联:

class Post < ApplicationRecord
  belongs_to :category
  # lets you call post.parent_category without dealing with nil errors
  has_one :parent_category, 
    through: :category
end

class Category < ApplicationRecord
  has_many :posts
  belongs_to :parent_category, 
    class_name: 'Category',
    optional: true
end

然后将代码干燥成一个部分,而不是用你的 ERB 做汤。

# app/views/posts/_breadcrumbs.html.erb
<div class="breadcrumbs">
  <% [post.parent_category, post.category].compact.each do |c| %>
    <%= link_to c.name, c %> &raquo;
  <% end %>
  <%= post.name %>
</div>

&raquo;好像 ”。如果你真的想使用>>你需要转义它&gt;&gt;或使用<%= content_tag(:span, '>>') %>as>在 HTML 中具有特殊意义。

然后在您想要的视图中渲染部分:

# app/views/posts/show.html.erb
<%= render partial: 'breadcrumbs', post: @post %>

推荐阅读