本文介绍了如何使用带有子类别的类别创建模型(使用Rails)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Rails创建一个博客.我通过多对多关系关联帖子和类别模型.如何在此模型中合并子类别?

I am creating a blog through Rails. I am relating the posts and categories models through a many-to-many relationship. How do I incorporate subcategories in this model?

推荐答案

> 层次结构 >

最好的方法是使用层次结构之一,通常是AncestryClosure_Tree宝石,为您的categories创建树"类型结构.然后,您就可以将blogs与每个类别相关联,从而创建所需的层次结构功能.

The best way is to use one of the hierarchy gems, typically Ancestry or Closure_Tree to create a "tree" type structure for your categories. You'll then be able to associate blogs with each category, creating the hierarchy functionality you need.

我们之前已经实现了-使用Ancestry gem:

We've achieved this before - using the Ancestry gem:

类别

从上图可以看到,您缺少的秘密调味料"是您的类别不在基于层次结构的结构中.

As you can see from the above image, the "secret sauce" you're missing is that your categories are not in a hierarchy-based structure.

之所以重要,是因为如果您将博客文章与特定的categories相关联,为了使这些类别实现所需的子类别"结构,您需要能够创建一个支持该类别的系统,因此是hierarchy宝石.

The reason this is important is because if you associate your blog posts with specific categories, in order for those categories to achieve the "sub category" structure you desire, you need to be able to create a system which supports it, hence the hierarchy gems.

具体来说,您当前正在创建一个单独的sub_category.rb模型.正确的方法是使用一个模型-Category-并使用层次结构gem为您提供树基础结构.

Specifically, you're currently working on creating a separate sub_category.rb model. The right way to do it is to keep it with one model - Category - and use the hierarchy gems to provide you with the tree infrastructure.

这是我们的操作方式:

#app/models/category.rb
class Category < ActiveRecord::Base
   has_ancestry #-> enables the "ancestry" gem
   has_and_belongs_to_many :posts
end

#app/models/post.rb
class Post < ActiveRecord::Base
   has_and_belongs_to_many :categories #-> you'll need a blogs_categories table with blog_id | category_id
end


控制器

从您的角度来看,要注意的重要一点是,您需要确保具有正确的控制器操作",才能为博客分配正确的类别:

The important thing to note from your perspective is that you will need to ensure you have the right "controller actions" in place, to assign the correct category to your blog:

#app/controllers/posts_controller.rb
class PostsController < ApplicationController
   def new
      @post = Post.new
   end

   def create
      @post = Post.new post_params
      @post.save
   end

    private

    def post_params
       params.require(:post).permit(:title, :body, :category_ids)
    end
end


查看

最后,此设置的真正魔力在于,如果您希望以层级/树格式显示项目,则可以使用Ancestry关联方法,使您能够创建嵌套"列表:

Finally, the real magic of this setup is that if you want to display your items in a hierachy / tree format, you'll be able to use the Ancestry association methods to give you the ability to create "nested" lists:

为此,您只需要使用部分调用,它将调用父对象的子对象,使您可以显示树"类型的层次结构(以及创建子类别):

To do this, you'll just need to use a partial, which will call the children of the parent objects, allowing you to show a "tree" type hierarchy (as well as creating subcategories):

#app/views/categories/_category.html.erb
<ol class="categories">
    <% collection.arrange.each do |category, sub_item| %>
        <li>
            <!-- Category -->
            <%= link_to category.title, edit_admin_category_path(category) %>

            <!-- Children -->
            <% if category.has_children? %>
                <%= render partial: "category", locals: { collection: category.children } %>
            <% end %>

        </li>
    <% end %>
</ol>

这将允许您调用以下内容:

This will allow you to call the following:

#app/views/categories/index.html.erb
<%= render partial: "category", locals: { collection: @categories } %>

这篇关于如何使用带有子类别的类别创建模型(使用Rails)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:20