ancestry 표시 방법
ancestry에 대해서는 이 기사에서 확인하십시오.
htps : // 아토라 1992. 하테나 bぉg. 코m/엔트리/2019/07/08/215444
작성의 편의상, view 파일은 haml로 기술하고 있습니다.
이번에는 카테고리를 이렇게 표시하고 싶은 경우입니다.

우선 controller를 기재합니다.
product 테이블로부터 상품 정보를 취득,
그런 다음 category 테이블에서 부모 요소를 가져옵니다.
省略
def show
@product = Product.find(params[:id])
@parents = Category.where(ancestry:nil)
end
省略
private
def product_params
params.require(:product)
end
end
여기가 view 파일입니다.
@parents를 each로 나눕니다.
그리고 product 테이블의 categories_id와 category 테이블의 id가 일치하는 경우, 표시하도록 기술합니다.
//親要素だけの場合
- @parents.each do |parent|
-if @product.categories_id == parent.id
= link_to '#' do
= parent.name
%br
//子要素までの場合
- parent.children.each do |child|
-if @product.categories_id == child.id
= link_to '#' do
= parent.name
%br
= link_to '#' do
= child.name
%br
//孫要素までの場合
- child.children.each do |grandchild|
-if @product.categories_id == grandchild.id
= link_to '#' do
= parent.name
%br
= link_to '#' do
= child.name
%br
= link_to '#' do
= grandchild.name
%br
여기에서 표시가 완료됩니다.
이번, 표시까지의 내용이므로, 링크처는 미기재입니다.
양해 바랍니다.
Reference
이 문제에 관하여(ancestry 표시 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/shimpex/items/f1e6f30b59845e41735e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)