ruby-on-rails 轨道:类别和子类别为轨道建模

46scxncf  于 2023-01-27  发布在  Ruby
关注(0)|答案(3)|浏览(127)

在不使用任何gem的情况下,我如何在rails中做到这一点?
主要类别
子类别
子类别
子类别
主要类别
子类别
子类别
子类别
主要类别
子类别
子类别
子类别
我有一张表,其中包括|身份证|第一级|第二级|
1级为主类别,2级为子类别
我想它显示在上面的视图一样。
在互联网上四处寻找之后,每个人似乎都推荐使用acts like-a-tree gem,但我想避免使用它们,因为我对rails相当陌生,我想了解如何做事情,而不是转向gem。
非常感谢你的帮助
型号:

class Category < ActiveRecord::Base
belongs_to :catalogue
    has_many :subcategories, :class_name => "Category", :foreign_key => "parent_id", :dependent => :destroy
belongs_to :parent_category, :class_name => "Category"
end

控制器:

class CataloguesController < ApplicationController
  layout 'main'
  def index
   @cats = Catalogue.all
  end

  def categories
   @cat = Catalogue.find(params[:id])
  end

end

查看:

<ul class="unstyled-list">

    <% @cat.categories.order([:level1]).each do |cat|%>
        <li><%=  cat.level1 %></li>
        <li><%= cat.level2 %></li>
    <% end %>
</ul>
yxyvkwin

yxyvkwin1#

创建一个子类别(或子子类别等)引用自身的模型:

class Category < ActiveRecord::Base
  has_many :subcategories, :class_name => "Category", :foreign_key => "parent_id", :dependent => :destroy
  belongs_to :parent_category, :class_name => "Category", :optional => true
end
  • has_many定义模型类型Categorysubcategories关联,即它使用相同的表。
  • belongs_to定义了返回父类别的关系,可通过@category.parent_category访问

有关模型关联has_manybelongs_to的更多信息,请阅读Associations Basics Guide
要使用此迁移创建表:

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :category do |t|
      t.string      :text
      # table name should be in plural 
      t.references  :parent_category, foreign_key: { to_table: :categories }
      t.timestamps
    end
  end
end
  • 注 *:此表格格式与您提议的格式(略有)不同,但我认为这不是一个真实的的问题。

Migrations Guide包含有关数据库迁移的详细信息。
在控制器中使用

def index
  @category = nil
  @categories = Category.find(:all, :conditions => {:parent_id => nil } )
end

查找没有父类别的所有类别(主类别
要查找任何给定类别的所有子类别,请用途:

# Show subcategory
def show
  # Find the category belonging to the given id
  @category = Category.find(params[:id])
  # Grab all sub-categories
  @categories = @category.parent_category
  # We want to reuse the index renderer:
  render :action => :index
end

要添加新类别,请用途:

def new
  @category = Category.new
  @category.parent_category = Category.find(params[:id]) unless params[:id].nil?
end

它创建一个新类别并设置父类别(如果提供了父类别)(否则它将成为主类别)
注意:我使用了旧的Rails语法(由于懒惰),但是对于现代版本的Rails,原理是相同的。
在你的categories/index.html.erb中,你可以使用如下代码:

<h1><%= @category.nil? ? 'Main categories' : category.text %></h1>
<table>
<% @categories.each do |category| %>
<tr>
  <td><%= link_to category.text, category_path(category) %></td>
  <td><%= link_to 'Edit', edit_category_path(category) unless category.parent.nil? %></td>
  <td><%= link_to 'Destroy', category_path(category), :confirm => 'Are you sure?', :method => :delete unless category.parent.nil? %></td>
</tr>
<% end %>
</table>
<p>
  <%= link_to 'Back', @category.parent_category.nil? ? categories_path : category_path(@category.parent_category) unless @category.nil? %>
  <%= link_to 'New (sub-category', new_category_path(@category) unless @category.nil? %>
</p>

它显示所选类别(或主类别)的名称及其所有子类别(在一个漂亮的表格中)。它链接到所有子类别,显示类似的布局,但子类别。最后,它添加了一个“新子类别”链接和一个“返回”链接。

j2qf4p5b

j2qf4p5b2#

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :category do |t|
      t.string      :text
      t.references  :parent
      t.timestamps
    end
  end
bt1cpqcv

bt1cpqcv3#

这就是你如何处理分类和课程或帖子

class Category < ApplicationRecord
    has_many :courses, dependent: :destroy 
    belongs_to :user
end

class Course < ApplicationRecord
    belongs_to :category

end

这是获取所有类别和课程的方法

def get_category_courses
         @category = Category.all.order(:name)
         @course =Category.joins(:courses).select('categories.id, courses.id as course_id, categories.name as category_name, courses.subject').
         group('categories.id, courses.id').paginate(page: params[:page],per_page:10)
         @courses= @course
          record_activity("#{@user} accessed category courses list  #{DateTime.now}")
          
         render json: {success: true, :category => @courses}
        end 
        end

这是查询的响应

{
    "success": true,
    "category": [
        {
            "id": 586513,
            "course_id": 8,
            "category_name": "Christianity",
            "subject": "English"
        },
        {
            "id": 586513,
            "course_id": 545,
            "category_name": "Christianity",
            "subject": "English"
        },
        {
            "id": 586513,
            "course_id": 4357,
            "category_name": "Christianity",
            "subject": null
        },
        {
            "id": 586513,
            "course_id": 5359,
            "category_name": "Christianity",
            "subject": "Cosmos"
        },
        {
            "id": 586513,
            "course_id": 6303,
            "category_name": "Christianity",
            "subject": "English"
        },
        {
            "id": 586513,
            "course_id": 6702,
            "category_name": "Christianity",
            "subject": "Algebra-X"
        },
        {
            "id": 586513,
            "course_id": 14317,
            "category_name": "Christianity",
            "subject": "Cosmos"
        },
        {
            "id": 586513,
            "course_id": 16699,
            "category_name": "Christianity",
            "subject": null
        },
        {
            "id": 586513,
            "course_id": 25384,
            "category_name": "Christianity",
            "subject": "Cosmos"
        },
        {
            "id": 586513,
            "course_id": 31644,
            "category_name": "Christianity",
            "subject": null
        }
    ]
}

相关问题