初学者です。
勉強のためフリマアプリのコピーサイトを作っています。
商品出品時に商品のカテゴリーのドロップダウンを実装しているところです。
メンズ >> トップス >>Tシャツ のように親要素が選択されたら子要素を表示し、、、という機能をjQueryを使用し作ったのですが、親子孫要素のカテゴリー名を配列型にしDBに保存がしたいです。(idではなく)
カテゴリー一覧はancestryを使いcategories テーブルに格納し controllerで親要素を取得、そこからjQueryで親要素が選択されたら子要素の選択ボックスが表示され、、という流れです。
選択ボックスの表示はf.selectを使用していますが,親要素しか渡せていなく、実装方法をご教授いただきたいです。
submitを押した際に配列で保存されるようにしたいです。
宜しくお願いします。
--new.html.haml
%h4 カテゴリー
.listing-product-detail__category
= f.select :categories, @category_parent_array, {}, {id: 'parent_category'}
--products_controller
def new
@product = Product.new
@product.images.build
@product.build_freight
@product.build_root_area
@product.build_day
@category_parent_array = ["---"]
Category.where(ancestry: nil).limit(13).each do |parent|
@category_parent_array << parent.name
end
end
def get_category_children
@category_children = Category.find_by(name: "#{params[:parent_name]}", ancestry: nil).children
end
def get_category_grandchildren
@category_grandchildren = Category.find("#{params[:child_id]}").children
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_path
else
render :new, notice: '保存できませんでした'
end
end
--category.js
$(document).on('turbolinks:load', function() {
$(function(){
function appendOption(category){
var html = <option value="${category.name}" data-category="${category.id}">${category.name}</option>
;
return html;
}
function appendChildrenBox(insertHTML){ var childSelecthtml = ''; childSelectHtml = `<div class='listing-select-wrapper__added' id= 'children_wrapper'> <select class="listing-select-wrapper__box--select" id="child_category" name="category_id"> <option value="---" data-category="---">---</option> ${insertHTML} <select> </div>`; $('.listing-product-detail__category').append(childSelectHtml); } function appendGrandchildrenBox(insertHTML){ var grandchildSelectHtml = `<div class='listing-select-wrapper__added' id= 'grandchildren_wrapper'> <select class="listing-select-wrapper__box--select" id="grandchild_category" name="category_id"> <option value="---" data-category="---">---</option> ${insertHTML} <select> </div>`; $('.listing-product-detail__category').append(grandchildSelectHtml); } $('#parent_category').on('change',function(){ var parentCategory = document.getElementById('parent_category').value; if (parentCategory != "---"){ $.ajax({ url: 'get_category_children', type: 'GET', data: { parent_name: parentCategory }, dataType: 'json' }) .done(function(children){ $('#children_wrapper').remove(); $('#grandchildren_wrapper').remove(); var insertHTML = ''; children.forEach(function(child){ insertHTML += appendOption(child); }); appendChildrenBox(insertHTML); }) .fail(function(){ alert('カテゴリー 取得に失敗しました') }) }else{ $('#children_wrapper').remove(); $('#grandchildren_wrapper').remove(); } }); $('.listing-product-detail__category').on('change', '#child_category',function(){ var childId = $('#child_category option:selected').data('category'); if (childId != "---"){ $.ajax({ url: 'get_category_grandchildren', type: 'GET', data: { child_id: childId }, dataType: 'json' }) .done(function(grandchildren){ if (grandchildren.length !=0) { $('#grandchildren_wrapper').remove(); var insertHTML = ''; grandchildren.forEach(function(grandchild){ insertHTML += appendOption(grandchild); }); appendGrandchildrenBox(insertHTML); } }) .fail(function(){ alert('カテゴリー 取得に失敗しました') }) }else{ $('#grandchildren_wrapper').remove(); } });
});
});
あなたの回答
tips
プレビュー