Rails 个子类别
Rails subcategories
如何查找和管理子类别? (我定义的 find_subcategory 方法似乎不起作用。)
class CategoriesController < ApplicationController
before_action :find_category, only: [:show]
def index
@categories = Category.order(:name)
end
def show
end
private
def find_category
@category = Category.find(params[:id])
end
def find_subcategory
@subcategory = Category.children.find(params[:parent_id])
end
end
我正在使用 acts_as_tree gem,它具有:
root = Category.create("name" => "root")
child1 = root.children.create("name" => "child1")
subchild1 = child1.children.create("name" => "subchild1")
root.parent # => nil
child1.parent # => root
root.children # => [child1]
root.children.first.children.first # => subchild1
不清楚您希望 find_subcategory 方法做什么,但是如果您希望它找到 params[:id] 中具有 id 的类别的所有子类别,则将其更改为
def find_subcategories
@subcategories = Category.where(:parent_id => params[:parent_id]).all
end
在您原来的版本中,您只是在寻找一个子类别,如果您只想要一个类别,您不妨从它的 ID 加载它。
我知道您接受了答案,但是 I've done this before 因此解释一下我们是如何做到的可能会有所帮助:
首先,我们使用了祖先gem。 我认为 acts_as_tree
is deprecated -- acts_as_tree
is better than ancestry
, I forgot why we used it now - ancestry
的工作方式非常 相似(parent
列,child
方法等)。
我将用 ancestry
解释我们的实现 - 希望它能给你一些关于 acts_as_tree
:
的想法
#app/models/category.rb
class Category < ActiveRecord::Base
has_ancestry #-> enables the ancestry gem (in your case it should be "acts_as_tree"
end
这将允许您填充 categories
模型中的 ancestry
(在您的情况下为 parent_id
)列,并且(最重要的是)使您能够 call the child
methods 附加到模型中的对象:
@category.parent
@category.children
...等等
--
这里要注意的重要一点是我们如何调用child
对象(即子类别 在你的情况下)。
您的方法是创建单独的对象并让它们相互继承。 ancestry
/ acts_as_tree
的美妙之处在于它们添加的方法。
任何具有正确 parent
ID 的对象都可以调用其 "children" 作为关联数据:
在我们的例子中,我们能够使用 ancetry
列关联所有对象。这比 acts_as_tree
稍微有点棘手,因为您必须在列中提供整个层次结构(这是蹩脚的),但结果仍然相同:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
end
#app/views/categories/index.html.erb
<%= render @categories %>
#app/views/categories/_category.html.erb
<%= category.name %>
<%= render category.children if category.has_children? %>
这将为您输出子类别:
How do I find and manage subcategories
你可以这样做:
@subcategories = Category.where parent_id: @category.id
或 如果您的祖先设置正确,您应该能够使用以下内容:
#config/routes.rb
resources :categories
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def show
@category = Category.find params[:id]
end
end
这将允许您使用:
#app/views/categories/show.html.erb
<% @category.children.each do |subcategory| %>
<%= subcategory.name %>
<% end %>
如何查找和管理子类别? (我定义的 find_subcategory 方法似乎不起作用。)
class CategoriesController < ApplicationController
before_action :find_category, only: [:show]
def index
@categories = Category.order(:name)
end
def show
end
private
def find_category
@category = Category.find(params[:id])
end
def find_subcategory
@subcategory = Category.children.find(params[:parent_id])
end
end
我正在使用 acts_as_tree gem,它具有:
root = Category.create("name" => "root")
child1 = root.children.create("name" => "child1")
subchild1 = child1.children.create("name" => "subchild1")
root.parent # => nil
child1.parent # => root
root.children # => [child1]
root.children.first.children.first # => subchild1
不清楚您希望 find_subcategory 方法做什么,但是如果您希望它找到 params[:id] 中具有 id 的类别的所有子类别,则将其更改为
def find_subcategories
@subcategories = Category.where(:parent_id => params[:parent_id]).all
end
在您原来的版本中,您只是在寻找一个子类别,如果您只想要一个类别,您不妨从它的 ID 加载它。
我知道您接受了答案,但是 I've done this before 因此解释一下我们是如何做到的可能会有所帮助:
首先,我们使用了祖先gem。 我认为 -- acts_as_tree
is deprecatedacts_as_tree
is better than ancestry
, I forgot why we used it now - ancestry
的工作方式非常 相似(parent
列,child
方法等)。
我将用 ancestry
解释我们的实现 - 希望它能给你一些关于 acts_as_tree
:
#app/models/category.rb
class Category < ActiveRecord::Base
has_ancestry #-> enables the ancestry gem (in your case it should be "acts_as_tree"
end
这将允许您填充 categories
模型中的 ancestry
(在您的情况下为 parent_id
)列,并且(最重要的是)使您能够 call the child
methods 附加到模型中的对象:
@category.parent
@category.children
...等等
--
这里要注意的重要一点是我们如何调用child
对象(即子类别 在你的情况下)。
您的方法是创建单独的对象并让它们相互继承。 ancestry
/ acts_as_tree
的美妙之处在于它们添加的方法。
任何具有正确 parent
ID 的对象都可以调用其 "children" 作为关联数据:
在我们的例子中,我们能够使用 ancetry
列关联所有对象。这比 acts_as_tree
稍微有点棘手,因为您必须在列中提供整个层次结构(这是蹩脚的),但结果仍然相同:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
end
#app/views/categories/index.html.erb
<%= render @categories %>
#app/views/categories/_category.html.erb
<%= category.name %>
<%= render category.children if category.has_children? %>
这将为您输出子类别:
How do I find and manage subcategories
你可以这样做:
@subcategories = Category.where parent_id: @category.id
或 如果您的祖先设置正确,您应该能够使用以下内容:
#config/routes.rb
resources :categories
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def show
@category = Category.find params[:id]
end
end
这将允许您使用:
#app/views/categories/show.html.erb
<% @category.children.each do |subcategory| %>
<%= subcategory.name %>
<% end %>