[英]Rails: categories and sub-categories model rails
在不使用任何寶石的情況下,我該如何在 Rails 中執行此操作?
主分類
子類別
子類別
子類別
主分類
子類別
子類別
子類別
主分類
子類別
子類別
子類別
我有一張由 | 組成的表編號 | 一級 | 2級 |
級別 1 是主要類別,級別 2 是子類別
我希望它顯示在上面的視圖中。
在 inte.net 上環顧四周之后,每個人似乎都建議使用 acts-like-a-tree gem,但我想避免使用它們,因為我對 Rails 還很陌生,我想了解如何做事而不是轉向到寶石。
非常感謝您的幫助
Model:
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
Controller:
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>
創建一個模型,該模型對子類別(或子子類別等)具有對自身的引用:
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
定義了模型類型Category
的subcategories
關聯。 即它使用相同的表。belongs_to
定義了一個與父類別的關系,可通過@category.parent_category
訪問有關模型關聯、 has_many
或belongs_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
注意:此表格格式與您建議的(略)不同,但我認為這不是真正的問題。
遷移指南包含有關數據庫遷移的更多信息。
在您的控制器中使用
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>
它顯示所選類別(或主類別)及其所有子類別的名稱(在一個漂亮的表格中)。 它鏈接到所有子類別,顯示類似的布局,但針對子類別。 最后,它添加了一個“新子類別”鏈接和一個“返回”鏈接。
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :category do |t|
t.string :text
t.references :parent
t.timestamps
end
end
這就是您可以做類別和課程或帖子的方式
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
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.