[JAVA] [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 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails 6] Implementation of search function
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 tagging function using intermediate table (without Gem)
[Rails] Implementation of many-to-many category functions
[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
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[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 retweet function in SNS application
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Note] Summary of rails login function using devise ①
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implementation of search function
Rails search function implementation
Implementation of pagination function
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
[Rails] Test of star evaluation function using Raty [Rspec]
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
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