在视图中显示顶级类别名称|铁轨上的红宝石

时间:2018-06-18 15:45:51

标签: ruby-on-rails ruby

我有blog_categoriessub_categories表,基于parent_id值,该值指向子类别上方的主要类别.id。主要类别的parent_idNULL。这一切都是通过自我指涉关系来完成的。

如何根据sub_category在blog_categories.show视图中显示当前blog_category和父/blog_categories/#的名称?

  

例如;

     
      
  • “新闻”.id 1和parent_id NULL,因为它是主要类别。
  •   
  • “好”.id 2和parent_id 1,因为它属于.id为1的类别。
  •   
  • “错误”.id 3和parent_id 1,因为它属于.id为1的类别。
  •   
     

当呈现/blog_categories/2时,我试图让它显示出来   子类别的名称(本例中为“好”),后跟其名称   父类别(在这种情况下,“新闻”)。

     

所需的结果是标题为“好消息”

BlogCategory 型号:

class BlogCategory < ApplicationRecord
  has_many :posts

  # This is called a self referential relation. This is where records in a table may point to other records in the same table.
  has_many :sub_categories, class_name: "BlogCategory", foreign_key: :parent_id

  # This is a scope to load the top level categories and eager-load their posts, subcategories, and the subcategories' posts too.
  scope :top_level, -> { where(parent_id: nil).includes :posts, sub_categories: :posts }
end

Blog_categories 控制器:

class BlogCategoriesController < ApplicationController

def index
  @categories = BlogCategory.top_level.includes(sub_categories: :posts)
  @category = BlogCategory.find_by_id(params[:id])
  unless @category.nil? # Allows for categories to have nothing in them, eliminating the NoMethodError
    @sub_category = @category.sub_categories.first
    @posts = @subcategory.posts
  end
  @all_posts = Post.all
end

def show
  @category = BlogCategory.find_by_id(params[:id])
  @sub_category = @category.sub_categories
  @posts = @category.posts
end

private

  def cat_params
    params.require(:blog_category).permit(:name, :parent_id, :sub_category)
  end

end

我的显示视图

<% BlogCategory.top_level do |category| %>
  <% category.sub_categories do |sub_category| %>
    <h2 class="center p-space blog-h2"><%= sub_category.name %> <%= category.name %></h2>
  <% end %>
<% end %>

我尝试了do语句的几种组合,但我真的无法解决这个问题。我很乐意帮助你解决这个问题,谢谢!

如果这有帮助,我在<%= @category.name %>取得了一些成功,但它只显示了sub_category的名称。

1 个答案:

答案 0 :(得分:1)

如果我没弄错,您需要显示当前类别名称+当前类别子类别名称。尝试根据您的需求调整此示例:

# controller
def show
  @category = BlogCategory.find(params[:id])
  if @category.present?
    @sub_cat_names = @category.sub_categories.map(&:name)
    @posts         = @category.posts
  end
end

# view
<% if @category.present? %>
  <h2>Current category:</h2> <%= @category.name %>
  <h3>Sub categories:</h3> <%= @sub_cat_names.join(', ') %>
<% end %>

<强>更新

如果您未在视图中使用这些变量,我不明白为什么在@category操作中需要@sub_categoryshow个变量。根据您最新添加的内容,解决方案可能如下所示:

# model
class BlogCategory < ApplicationRecord
  has_many :posts
  has_many :sub_categories, class_name: 'BlogCategory', foreign_key: :parent_id

  belongs_to :parent, class_name: 'BlogCategory'

  scope :top_level, -> { where(parent_id: nil).includes :posts, sub_categories: :posts }
end

# controller
def show
  @sub_category = BlogCategory.find(params[:id])
  @category     = @sub_category.parent
end

# view
<h2 class="center p-space blog-h2">
  <%= "#{@sub_category.name} #{@category.name}" %>
</h2>