前提・実現したいこと
投稿機能にancestryを用いたカテゴリー機能を実装
投稿に失敗し、カテゴリーを選び直したら以下のようなエラーが発生しました。
発生している問題・エラーメッセージ
カテゴリーを選び直したらJavaScriptが動きません。
Started GET "/recruitments/8/get_category_children?parent_id=1" for ::1 at 2021-11-22 12:44:45 +0900 ActionController::RoutingError (No route matches [GET] "/recruitments/8/get_category_children"):
該当のソースコード
JavaScript
1window.addEventListener('DOMContentLoaded', function(){ 2 function appendOption(category){ 3 var html = `<option value="${category.id}">${category.name}</option>`; 4 return html; 5 } 6 function appendChildrenBox(insertHTML){ 7 var childSelectHtml = ""; 8 childSelectHtml = `<div class="category__child" id="children_wrapper"> 9 <select id="child__category" name="recruitment[category_id]" class="serect_field"> 10 <option value="">---</option> 11 ${insertHTML} 12 </select> 13 </div>`; 14 $('.append__category').append(childSelectHtml); 15 } 16 function appendGrandchildrenBox(insertHTML){ 17 var grandchildSelectHtml = ""; 18 grandchildSelectHtml = `<div class="category__child" id="grandchildren_wrapper"> 19 <select id="grandchild__category" name="recruitment[category_id]" class="serect_field"> 20 <option value="">---</option> 21 ${insertHTML} 22 </select> 23 </div>`; 24 $('.append__category').append(grandchildSelectHtml); 25 } 26 27 $('#recruitment_category_id').on('change',function(){ 28 var parentId = document.getElementById('recruitment_category_id').value; 29 if (parentId != ""){ 30 $.ajax({ 31 url: 'get_category_children', 32 type: 'GET', 33 data: { parent_id: parentId }, 34 dataType: 'json' 35 // cache: false, 36 }) 37 38 .done(function(children){ 39 $('#children_wrapper').remove(); 40 $('#grandchildren_wrapper').remove(); 41 var insertHTML = ''; 42 children.forEach(function(child){ 43 insertHTML += appendOption(child); 44 }); 45 appendChildrenBox(insertHTML); 46 }) 47 .fail(function(){ 48 alert('カテゴリー取得に失敗しました'); 49 }) 50 }else{ 51 $('#children_wrapper').remove(); 52 $('#grandchildren_wrapper').remove(); 53 } 54 }); 55 56 $('.append__category').on('change','#child__category',function(){ 57 var childId = document.getElementById('child__category').value; 58 if(childId != "" ){ 59 $.ajax({ 60 url: '/recruitments/get_category_grandchildren', 61 type: 'GET', 62 data: { child_id: childId }, 63 dataType: 'json' 64 // cache: false, 65 }) 66 .done(function(grandchildren){ 67 $('#grandchildren_wrapper').remove(); 68 var insertHTML = ''; 69 grandchildren.forEach(function(grandchild){ 70 insertHTML += appendOption(grandchild); 71 }); 72 appendGrandchildrenBox(insertHTML); 73 }) 74 .fail(function(){ 75 alert('カテゴリー取得に失敗しました'); 76 }) 77 }else{ 78 $('#grandchildren_wrapper').remove(); 79 } 80 }) 81});
routes
1 2Rails.application.routes.draw do 3 devise_for :users 4 root to: "recruitments#index" 5 resources :users 6 # resources :categories 7 resources :recruitments do 8 collection do 9 get 'get_category_children', defaults: { format: 'json' } 10 get 'get_category_grandchildren', defaults: { format: 'json' } 11 get 'search' 12 get 'post_done' 13 get 'delete_done' 14 get 'detail_search' 15 get 'update_done' 16 end 17 resources :orders, only: [:index, :create] 18 end 19end
controller
1可読性向上のためコードの抜粋をしております。 2 3class RecruitmentsController < ApplicationController 4 before_action :authenticate_user!, only: [:new, :edit, :destroy] 5 before_action :set_array, only: [:new, :create, :show, :edit] 6 before_action :set_recruitment, only: [ :edit, :update, :show, :destroy] 7 8 def index 9 @recruitments = Recruitment.order("created_at DESC") 10 end 11 12 def new 13 @recruitment = Recruitment.new 14 end 15 16 def create 17 @recruitment = Recruitment.new(recruitment_params) 18 if @recruitment.save 19 redirect_to root_path(@recruitment) 20 else 21 render :new 22 end 23 end 24 25 def get_category_children 26 @category_children = Category.find("#{params[:parent_id]}").children 27 end 28 29 def get_category_grandchildren 30 @category_grandchildren = Category.find("#{params[:child_id]}").children 31 end 32 33 private 34 35 def recruitment_params 36 params.require(:recruitment).permit(:heading, :content, :category_id, :price, :image).merge(user_id: current_user.id) 37 end 38 39 def set_array 40 @category_parent_array = Category.where(ancestry: nil) 41 end 42 43 def set_recruitment 44 @recruitment = Recruitment.find(params[:id]) 45 end 46 47end
html
1 2 <%# カテゴリー %> 3 <div class="append__category"> 4 <div class="weight-bold-text"> 5 カテゴリー 6 <span class="indispensable">必須</span> 7 </div> 8 <div class="fform_label"> 9 <div class="lavel_name "> 10 <%= f.collection_select(:category_id, @category_parent_array, :id, :name, {include_blank: "選択してください"}, class:"select-box", id: "recruitment_category_id") %> 11 </div> 12 </div> 13 </div>
試したこと
https://jisakupc-technical.info/programing/jquery/4110/
こちらの記事を見て、IEの設定を変えようと試みましたがwindowsしか対応していないのでしょうか?
設定方法がわからず断念しました。
補足情報(FW/ツールのバージョンなど)
railsのバージョン
Rails 6.0.4.1
gemのバージョン
3.0.3
回答1件
あなたの回答
tips
プレビュー