<%= simple_form_for @product, method: :post, url: save_item_path do |f| %>
<tr>
<td>
Category
</td>
<td>
<%= f.input :category_id, collection: Category.all, label: false, prompt: 'Select your category', required: true ,required: true %>
</td>
</tr>
<div class="row " style="height:100px;width:100%">
<div class="col-md-5 sub-cat-dropdown">
<select class="select required sub-cat" required="required" name="product[sub_id]"><option value="">Select your sub category</option><option value="15">Bottle</option><option value="16">Charger</option></select>
</div>
</div>
<div class="row">
<div class="col-md-3 col-md-offset-5">
<%= f.submit :submit %>
</div>
</div>
<% end %>
The form is submitted with all the necessary parameters .. here are the pairs
{"utf8"=>"✓", "authenticity_token"=>"4gY7lWgSqo6j4UoZ78iv1nsaMq9TUXLXE2oQzlqqyh3I3oED3CxwM04cB1n5P+Nz81O3Vvk+/NDPxixNey/J9Q==", "product"=>{"name"=>"vamsi", "price"=>"456", "availability"=>"true", "about"=>"aerg", "ref"=>"456", "texture"=>"rdgg", "shipping"=>"ewataewrg", "category_id"=>"13", "notes"=>"feartg", "packaging"=>"wer5tyy", "sub_id"=>"15"}, "commit"=>"submit", "controller"=>"items", "action"=>"create"}
In the hash parameter above, you can send sub_id.
However, a drop-down list of subcategories should be generated dynamically based on the choice of the main category.
So the sub-cat-dropdown div is empty as follows
<div class="row " style="height:100px;width:100%">
<div class="col-md-5 sub-cat-dropdown">
</div>
</div>
I am trying to enter a select tag inside a drop-down list of a subdirectory by clicking the drop-down menu of the main category
Here is the code I wrote
$(document).ready(function(){
$('#product_category_id').on('change', function(){
var values = [];
var inner_contents = [];
if(this.value === undefined){
return;
}else{
$main_category = this.value;
$main_category_length = $cat_obj[$main_category].length;
for (var i=0;i<$main_category_length;i++){
values.push($cat_obj[$main_category][i].id);
inner_contents.push($cat_obj[$main_category][i].name);
}
$('.sub-cat-dropdown').empty();
inject_sub_category(values, inner_contents);
}
});
function inject_sub_category(value_array, inner_html_array){
var select = document.createElement("select");
select.setAttribute("class","select required sub-cat");
select.setAttribute("required","required");
select.setAttribute("name","sub_id");
$('.sub-cat-dropdown').append(select);
var default_option = document.createElement("option");
default_option.setAttribute("value","")
default_option.innerHTML = default_option.innerHTML + "Select your sub category";
select.appendChild(default_option);
for(var i = 0 ; i< value_array.length ; i++){
var option_element = document.createElement("option");
option_element.setAttribute("value",value_array[i]);
option_element.innerHTML = option_element.innerHTML + inner_html_array[i];
select.appendChild(option_element)
}
}
$.ajax({
url: "http://localhost:3000/categories_api/show",
type: 'GET',
dataType: 'json',
success: function(res) {
$cat_obj = res;
}
});
});
What the above code does is to assume that it should insert a select dropdown. inside the dropdown menu
Paste select tag looks like copied from browser
<div class="col-md-5 sub-cat-dropdown" style="-webkit-user-select: text;"><select class="select required sub-cat" required="required" name="product[sub_id]"><option value="">Select your sub category</option><option value="15">Bottle</option><option value="16">Charger</option></select></div>
But, when form submission occurs, params does not have sub_id
{"utf8"=>"✓", "authenticity_token"=>"zUVF55KdvQ4l8nBjtlIcofqHECgd3/pIi7U97cF0xbrnnf9xJqNns8gPPSOgpVAEcs6V0bewdE9XGQFu4PHGUg==", "product"=>{"name"=>"jdkrfb", "price"=>"89", "availability"=>"true", "about"=>"sfukb", "ref"=>"78678", "texture"=>"jkdfbjkrbg", "shipping"=>"jkdfsk", "category_id"=>"12", "notes"=>"jhhdfvbj", "packaging"=>"sajkf"}, "commit"=>"submit", "controller"=>"items", "action"=>"create"}
, .
:
ajax , select. , DOM select sub-cat-dropdown.
sub_id , .