【问题标题】:Displaying top level category name in view | ruby on rails在视图中显示顶级类别名称 |铁轨上的红宝石
【发布时间】:2018-06-18 15:45:50
【问题描述】:

我有一个blog_categories 表,其中sub_categories 基于指向主类别.idparent_id 值,该值位于子类别之上。主要类别有 parent_idNULL。这都是通过自指关系完成的。

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

例如;

  • “新闻”拥有.id 1 和parent_id NULL,因为它是主要类别。
  • “Good” 具有 .id 2 和 parent_id 1,因为它属于 .id 为 1 的类别。
  • “坏” 具有 .id 3 和 parent_id 1,因为它属于 .id 为 1 的类别。

/blog_categories/2 被渲染时,我试图让它显示 子类别的名称(在本例中为 "Good"),后跟其 父类别(在本例中为“新闻”)。

期望的结果是一个标明“好消息”

的标题

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 语句的几种组合,但我真的无法解决这个问题。我希望得到一些帮助来解决这个问题,谢谢!

如果这有帮助,我在 &lt;%= @category.name %&gt; 上取得了一些成功,但它只显示子类别的名称。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案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 %>
    

    更新

    我不明白如果您不在视图中使用它们,为什么在 show 操作中需要 @category@sub_category 变量。根据您的最新添加,解决方案可能如下所示:

    # 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>
    

    【讨论】:

    • 嘿,谢谢,但是这个实际上只是列出了列表中的每个子类别和类别。我的目标是,当在 URL 上时:例如,/blog_categories/6,它将显示子类别,后跟它所属的类别。
    • 我认为它很接近,但这个实际上列出了每一列的每个值。 (甚至更多信息)我不知道为什么,它正在拉动.erb中甚至没有要求的东西。
    • @Jake 对不起,你不需要使用等号,我的错。请再次检查我的代码。它应该可以正常工作,我刚刚在我的应用程序中检查了类似的示例。并且请对您需要的内容进行一些截图,很难想象您的预期结果。
    • 我已经尝试了您刚刚更新的代码,但它根本没有加载任何内容。这会是我的控制器中可能遇到的问题吗?此代码在show 视图中
    • @Jake 再次 - 我不确定我是否正确理解您的需求。因此,请尝试更新代码,如果有问题,请使用更多详细信息和预期结果示例更新您的问题。
    猜你喜欢
    • 2011-07-13
    • 2016-01-07
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多