[Rails]ancestry를 사용했을 때의 약간의 손잡이에서 배운 것
소개
개인 제작으로 패션계 EC 사이트를 제작했을 때에 「계층이 있는 컬럼」을 실장하고 싶고, 바로 그것을 실현해 주는 ancestry를 사용했을 때의 약간의 손잡이로부터 배운 것을 비망록으로서 남겨 둡니다 .
또, 본 기사는 순서서가 아니고, 초학자가 빠지기 십상인 실패의 소개입니다!
※실장의 순서등은 할애합니다. Qiita에도 많이 다른 분의 기사가 있으므로 이하 참고로 해 주시면 좋겠습니다.
ancestry에 의한 다계층 구조 데이터를 이용해 동적 카테고리 선택 박스 실현 ~Ajax~
ancestry로 만든 선택 상자에 초기 값을 표시하는 방법
실현하고 싶은 것
ancestry를 이용한 이하의 동적인 셀렉트 박스를 편집시에도 유효하게 하고 싶습니다.

전제
다음과 같이 컨트롤러에서 하위 계층을 검색하는 작업을 정의한다고 가정합니다.
ajax도 제대로 search_child를 정의합니다.
(controllers/items_controller.rb)
(省略)
def edit
child_category = @item.category
@category_parent_ary = []
Category.where(ancestry: nil).each do |parent|
@category_parent_ary << parent
end
@category_children_ary = []
Category.where(ancestry: child_category.ancestry).each do |children|
@category_children_ary << children
end
end
def search_child
respond_to do |format|
format.html
format.json do
@children = Category.find(params[:parent_id]).children
end
end
end
(省略)
end
(省略)
$("#parent-form").on("change", function() {
var parentValue = document.getElementById("parent-form").value;
if (parentValue != "---") {
$('#category__box--children').remove();
$.ajax({
url : 'search_child',
type : 'GET',
data : { parent_id: parentValue },
dataType: 'json'
})
(省略)
편집시 검색하지 않습니다! ?

여러가지, 실장해 신규 등록으로 되어 있고, 아마 문제 없을 것이라고 생각하고 있었으므로, 프리즈 했습니다.
해결
매우 간단하고 라우팅을 추가했습니다.
(config/routes.rb)
Rails.application.routes.draw do
(省略)
resources :shops, only: [:new, :create, :show, :edit, :update, :destroy] do
resources :items, only: [:new, :create, :edit, :update, :destroy] do
collection do
get "search_child", defaults: { format: "json" }
end
#####↓ここを追加↓####
member do
get "search_child", defaults: { format: "json" }
end
#####↑ここを追加↑####
end
(省略)
교훈
그만 1기능의 실장 범위가 부풀어 오르면 발밑을 흔들어 경향이 있어, 이번의 에러도 쓴 코드의 내용만에 눈이 가 버려, 나름대로 시간을 녹였습니다. .
다시 동작할 때까지의 코드의 흐름을 제대로 이해해 의심의 눈을 돌리는 것이 중요하다고, 반성입니다!
Reference
이 문제에 관하여([Rails]ancestry를 사용했을 때의 약간의 손잡이에서 배운 것), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/fumiya1753/items/559144fe6bb7f44a11e5
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
ancestry를 이용한 이하의 동적인 셀렉트 박스를 편집시에도 유효하게 하고 싶습니다.

전제
다음과 같이 컨트롤러에서 하위 계층을 검색하는 작업을 정의한다고 가정합니다.
ajax도 제대로 search_child를 정의합니다.
(controllers/items_controller.rb)
(省略)
def edit
child_category = @item.category
@category_parent_ary = []
Category.where(ancestry: nil).each do |parent|
@category_parent_ary << parent
end
@category_children_ary = []
Category.where(ancestry: child_category.ancestry).each do |children|
@category_children_ary << children
end
end
def search_child
respond_to do |format|
format.html
format.json do
@children = Category.find(params[:parent_id]).children
end
end
end
(省略)
end
(省略)
$("#parent-form").on("change", function() {
var parentValue = document.getElementById("parent-form").value;
if (parentValue != "---") {
$('#category__box--children').remove();
$.ajax({
url : 'search_child',
type : 'GET',
data : { parent_id: parentValue },
dataType: 'json'
})
(省略)
편집시 검색하지 않습니다! ?

여러가지, 실장해 신규 등록으로 되어 있고, 아마 문제 없을 것이라고 생각하고 있었으므로, 프리즈 했습니다.
해결
매우 간단하고 라우팅을 추가했습니다.
(config/routes.rb)
Rails.application.routes.draw do
(省略)
resources :shops, only: [:new, :create, :show, :edit, :update, :destroy] do
resources :items, only: [:new, :create, :edit, :update, :destroy] do
collection do
get "search_child", defaults: { format: "json" }
end
#####↓ここを追加↓####
member do
get "search_child", defaults: { format: "json" }
end
#####↑ここを追加↑####
end
(省略)
교훈
그만 1기능의 실장 범위가 부풀어 오르면 발밑을 흔들어 경향이 있어, 이번의 에러도 쓴 코드의 내용만에 눈이 가 버려, 나름대로 시간을 녹였습니다. .
다시 동작할 때까지의 코드의 흐름을 제대로 이해해 의심의 눈을 돌리는 것이 중요하다고, 반성입니다!
Reference
이 문제에 관하여([Rails]ancestry를 사용했을 때의 약간의 손잡이에서 배운 것), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/fumiya1753/items/559144fe6bb7f44a11e5
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
(controllers/items_controller.rb)
(省略)
def edit
child_category = @item.category
@category_parent_ary = []
Category.where(ancestry: nil).each do |parent|
@category_parent_ary << parent
end
@category_children_ary = []
Category.where(ancestry: child_category.ancestry).each do |children|
@category_children_ary << children
end
end
def search_child
respond_to do |format|
format.html
format.json do
@children = Category.find(params[:parent_id]).children
end
end
end
(省略)
end
(省略)
$("#parent-form").on("change", function() {
var parentValue = document.getElementById("parent-form").value;
if (parentValue != "---") {
$('#category__box--children').remove();
$.ajax({
url : 'search_child',
type : 'GET',
data : { parent_id: parentValue },
dataType: 'json'
})
(省略)

여러가지, 실장해 신규 등록으로 되어 있고, 아마 문제 없을 것이라고 생각하고 있었으므로, 프리즈 했습니다.
해결
매우 간단하고 라우팅을 추가했습니다.
(config/routes.rb)
Rails.application.routes.draw do
(省略)
resources :shops, only: [:new, :create, :show, :edit, :update, :destroy] do
resources :items, only: [:new, :create, :edit, :update, :destroy] do
collection do
get "search_child", defaults: { format: "json" }
end
#####↓ここを追加↓####
member do
get "search_child", defaults: { format: "json" }
end
#####↑ここを追加↑####
end
(省略)
교훈
그만 1기능의 실장 범위가 부풀어 오르면 발밑을 흔들어 경향이 있어, 이번의 에러도 쓴 코드의 내용만에 눈이 가 버려, 나름대로 시간을 녹였습니다. .
다시 동작할 때까지의 코드의 흐름을 제대로 이해해 의심의 눈을 돌리는 것이 중요하다고, 반성입니다!
Reference
이 문제에 관하여([Rails]ancestry를 사용했을 때의 약간의 손잡이에서 배운 것), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/fumiya1753/items/559144fe6bb7f44a11e5
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
(config/routes.rb)
Rails.application.routes.draw do
(省略)
resources :shops, only: [:new, :create, :show, :edit, :update, :destroy] do
resources :items, only: [:new, :create, :edit, :update, :destroy] do
collection do
get "search_child", defaults: { format: "json" }
end
#####↓ここを追加↓####
member do
get "search_child", defaults: { format: "json" }
end
#####↑ここを追加↑####
end
(省略)
그만 1기능의 실장 범위가 부풀어 오르면 발밑을 흔들어 경향이 있어, 이번의 에러도 쓴 코드의 내용만에 눈이 가 버려, 나름대로 시간을 녹였습니다. .
다시 동작할 때까지의 코드의 흐름을 제대로 이해해 의심의 눈을 돌리는 것이 중요하다고, 반성입니다!
Reference
이 문제에 관하여([Rails]ancestry를 사용했을 때의 약간의 손잡이에서 배운 것), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/fumiya1753/items/559144fe6bb7f44a11e5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)