Hierarchy
The best way is to use one of the stones in the hierarchy, usually Ancestry
or Closure_Tree
, to create a tree structure for yours categories
. You can then associate blogs
with each category, creating the hierarchy functionality you need.
We achieved this earlier - using stone Ancestry
:
Categories
, " ", , , .
, , , , categories
, "", , , , hierarchy
.
, sub_category.rb
. - - Category
- , .
:
class Category < ActiveRecord::Base
has_ancestry
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
, , " " , :
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
, "" :
, , ( ):
#app/views/categories/_category.html.erb
<ol class="categories">
<% collection.arrange.each do |category, sub_item| %>
<li>
<%= link_to category.title, edit_admin_category_path(category) %>
<% if category.has_children? %>
<%= render partial: "category", locals: { collection: category.children } %>
<% end %>
</li>
<% end %>
</ol>
:
<%= render partial: "category", locals: { collection: @categories } %>