jQueryで取得した値をformで保存したい
受付中
回答 0
投稿
- 評価
- クリップ 1
- VIEW 666
初学者です。
勉強のためフリマアプリのコピーサイトを作っています。
商品出品時に商品のカテゴリーのドロップダウンを実装しているところです。
メンズ >> トップス >>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();
}
});
});
});
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.34%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正の依頼
yambejp
2019/11/08 10:21
フロントとバックの切り分けがわかるように質問ください
jQueryはデータを構造化して送り出すところまでです。
フロント側がわからないなら、具体的なhtmlソースと
期待するデータ構造を例示ください。
バック側はget/postもしくはファイルを受け取り
ファイルもしくはRDBに保存することになるでしょう