简体   繁体   English

Ruby on Rails - # 的未定义方法`category&#39;<Post:0x000055a44cf6bd00> 你的意思是? 类别编号

[英]Ruby on Rails - undefined method `category' for #<Post:0x000055a44cf6bd00> Did you mean? category_id

Error Message Screenshot I am learning Ruby on Rails in college right now and I am struggling with it.错误消息屏幕截图我现在正在大学学习 Ruby on Rails,但我正在为此苦苦挣扎。 I have a project to be finished in the next days but I cannot find out what is wrong with my code, I have tried to fix for it, I googled it already but no luck.我有一个项目要在接下来的几天内完成,但我找不到我的代码有什么问题,我试图修复它,我已经用谷歌搜索了它但没有运气。

Due to the coronavirus the educational institutions are closed and I am unable to ask my lecturer for assistance on this.由于冠状病毒,教育机构关闭,我无法向我的讲师寻求帮助。

I would appreciate if someone could help me out on this?如果有人能帮我解决这个问题,我将不胜感激?

My application is very simple, it is a place where people can post whatever they want, however, they need to choose a category and other people can comment on it.我的应用程序很简单,它是一个人们可以发布他们想要的任何内容的地方,但是,他们需要选择一个类别,其他人可以对其发表评论。

The error I am getting displayed on the browser is the below:我在浏览器上显示的错误如下:

undefined method 'category' for # #的未定义方法“类别”

Did you mean?你的意思是? category_id类别编号

Trace of template inclusion: #模板包含的痕迹:#

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

Request要求

Parameters:参数:

{"id"=>"3"} {“id”=>“3”}

My code where the error is been displayed is the below:我显示错误的代码如下:

Below is the /app/views/layouts/_breadcrumb_nav.html.erb下面是/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>

Below is the /app/views/layouts/application.html.erb下面是/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>

Below is the /app/models/post.rb下面是/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

I have a little bit of knowledge of Java and Ruby on Rails is killing me and I hope you can help me out.我对 Java 和 Ruby on Rails 有一点了解,我希望你能帮助我。

Many thanks Richard非常感谢理查德

I'm guessing your trying to to build something like a breadcrumb navigation that looks something like this:我猜您正在尝试构建类似如下所示的面包屑导航:

Authors >> French >> Albert Camus

Start by setting up the associations:首先设置关联:

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

Then DRY the code into a partial instead of making soup out of your ERB.然后将代码干燥成部分代码,而不是用 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; looks like ».好像 ”。 If you actually want to use >> you need to escape it &gt;&gt;如果你真的想使用>>你需要转义它&gt;&gt; or use <%= content_tag(:span, '>>') %> as > has special significance in HTML.或使用<%= content_tag(:span, '>>') %>因为>在 HTML 中具有特殊意义。

Then render the partial in the view where you want it:然后在您想要的视图中渲染部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 #的未定义方法`category_id&#39; <Post:0xade8ce4> - undefined method `category_id' for #<Post:0xade8ce4> Ruby on Rails:User_id和category_id为nil - Ruby on Rails: User_id and category_id is nil 什么是ArticlesController#show中的NoMethodError? nil:NilClass的未定义方法“ category_id”是什么? - What isNoMethodError in ArticlesController#show ? and what is undefined method `category_id' for nil:NilClass? 我不断收到此错误<undefined method `category_id' for #<ActionController::Parameters> &gt; - I keep getting this error <undefined method `category_id' for #<ActionController::Parameters>> Rails 路由 /forum/[category_id]/[topic_id] - Rails routing /forum/[category_id]/[topic_id] nameerror未定义的局部变量或方法`log_out&#39;是什么意思? logout_url红宝石在轨道上 - nameerror undefined local variable or method `log_out' Did you mean? logout_url ruby on rails 没有路由匹配{:action =&gt;“ show_post_category”,:category_id =&gt; 5,:controller =&gt;“ posts”} - No route matches {:action=>“show_post_category”, :category_id=>5, :controller=>“posts”} 在Ruby on Rails中按帖子类别分开 - Separating By Post Category in Ruby on Rails 使用选择器ROR更改category_id - change category_id with a selector ROR 使用特定的:category_id渲染部分 - Rendering Partial with specific :category_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM