[Rails] Implementation of multi-layer category function using ancestry "Editing form"

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3Introduction of Font Awesome -Login function implementationImplementation of posting function -Many-to-many category function implementationMulti-layer category function implementation (preparation)Multi-layer category function implementation (seed) ・ [Multi-layer category function implementation (creation form)] (https://qiita.com/matsubishi5/items/4afb4a4f307023126c66)

Implementation

1. Edit the controller

books_controller.rb


def edit
  unless @book.user == current_user
    redirect_to books_path
  end
  @category_parent_array = Category.category_parent_array_create
end

def update
  if @book.update(book_params)
    book_categories = BookCategory.where(book_id: @book.id)
    book_categories.destroy_all
    BookCategory.maltilevel_category_create(
      @book,
      params[:parent_id],
      params[:children_id],
      params[:grandchildren_id]
    )
    redirect_to @book
  else
    @category_parent_array = Category.category_parent_array_create
    render 'edit'
  end
end

[Explanation]

(1) Extract all the records corresponding to the book to be edited from the intermediate table and delete them.

book_categories = BookCategory.where(book_id: @book.id)
book_categories.destroy_all

2. Edit the view

slim:books/edit.html.slim


/Postscript
.category-form
  = label_tag 'Genre'
  = select_tag 'parent_id', options_for_select(@category_parent_array), class: 'form-control', id: 'parent-category'
  i.fas.fa-chevron-down
br

Caution

If you do not disable turbolinks, the select box will not work asynchronously, so be sure to disable it.

How to disable turbolinks

Sequel

Multi-layer category function implementation (I tried to make a window with Bootstrap)

Recommended Posts

[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of category function
[Rails] gem ancestry category function implementation
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
[Rails] Implementation of search function using gem's ransack
[Rails] Implementation of image enlargement function using lightbox2
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Implementation of many-to-many category functions
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Implementation of user authentication function using devise (3)
[Ruby on rails] Implementation of like function
[Rails] Category function
[Rails] I will explain the implementation procedure of the follow function using form_with.
Implementation of category selection form using JS in flea market application creation
Implementation of Ruby on Rails login function (Session)
Implement the product category function using ancestry ① (Preparation)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
Implementation of search function
Implementation of pagination function
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
Rails implementation of ajax removal
Rails fuzzy search function implementation
Search function using [rails] ransack
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
Implementation of sequential search function
Implementation of like function (Ajax)
Implementation of image preview function
Rails sorting function implementation (displayed in order of number of like)
Implement category function using ancestory
[Rails] Japanese localization of error message when using Form object
[Implementation procedure] Create a user authentication function using sorcery in Rails
Let's create a TODO application in Java 8 Implementation of editing function
[Rails] [jQuery] Asynchronous like function implementation using remote: true and js.erb
[Rails] Implementation of user logic deletion
[Rails] Tag management function (using acts-as-taggable-on)
Implementation of validation using regular expressions
[Ruby on Rails] Comment function implementation
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
[Android] Implementation of side-scrolling ListView using RecyclerView
Rails Addition of easy and easy login function
[Ruby on Rails] Follow function implementation: Bidirectional
Rails Basic CRUD function implementation procedure scaffold