[RUBY] [Rails] What I learned from a little stumbling block when using ancestry

Introduction

As a memorandum, I wanted to implement a "column with a hierarchy" when I made a fashion e-commerce site by personal production, and I learned from a little stumbling block when using ** ancestry ** that realizes exactly that. I will leave it.

** Also, this article is not a procedure manual, but an introduction to the mistakes that beginners tend to make! ** **

What you want to achieve

I want to enable the following dynamic select box using ancestry ** even when editing **.

Image from Gyazo

Premise

It is assumed that the controller defines the action to search the child hierarchy as shown below. Ajax also defines search_child firmly.

(controllers/items_controller.rb)

(abridgement)

  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

(abridgement)

end

(abridgement)

  $("#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'
      })

(abridgement)

Doesn't search when editing! ??

Image from Gyazo

I implemented it and newly registered it, and I thought that there would be no problem, so I froze it.

Solution

It's very simple, I just added the routing.

(config/routes.rb)
Rails.application.routes.draw do

(abridgement)

  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
     #####↓ Add here ↓ #####
      member do
        get "search_child", defaults: { format: "json" }
      end
    #####↑ Add here ↑ #####
    end
(abridgement)

Lesson

When the implementation range of one function expands, I tend to scoop up my feet, and I just focused on the content of the code I wrote for this error, and I spent some time. ..

I regret that it is important to understand the flow of the code until it works again and to look at the doubts!

Recommended Posts

[Rails] What I learned from a little stumbling block when using ancestry
What I learned from studying Rails
What I learned when building a server in Java
[Beginner] What I learned when trying to introduce bootstrap to the rails6 app without using a CDN [Asset pipeline]
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned from Java monetary calculation
What I don't like when using interface of a function with default arguments in Kotlin from Java
[Personal memo] I learned a little about modifiers
[Rails] I made a draft function using enum
What i learned
I want to use a little icon in Rails
What to do when rails creates a 〇〇 2.rb file
What I learned ② ~ Mock ~
What I learned ① ~ DJUnit ~
[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!
What I was addicted to while using rspec on rails
I entered from Rails and didn't know what [attr_accessor] was
I tried using Hotwire to make Rails 6.1 scaffold a SPA
Beginner Ruby on Rails What I learned is being summarized
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
A note of what I stumbled upon and noticed in catching up with Laravel from Rails
Error when using rails capybara
Detailed tips when using Rails
What I learned about Kotlin
I can't log in to MySQL from Django when using docker-compose
About what I did when creating a .clj file in Clojure
I get a Ruby version error when I try to start Rails.
What I was addicted to when implementing google authentication with rails
I tried to build a simple application using Dockder + Rails Scaffold
What I learned from doing Java work with Visual Studio Code
What I thought about when I started migrating from Java to Kotlin