Edited: I am trying to achieve cascading dropdown.
In my 1st dropdown, I get all the distinct Names.
<%= f.input :names, collection: names.distinctnames, :label => "Select Name" %>
On selecting the name, how to access the value and pass it to controller/model so that I can filter based on the values and bind it to the next dropdown.
In my Model, I have the following scope
scope :distinctnames, ->{ Names.distinct.pluck(:names)}
Here, I want to add another scope that gives the cities for the selected name.
So, how would I get the data selected in my view and get all the values in the next dropdown.
If this is the wrong approach, can someone suggest me the alternative one with and example.
My code
<!DOCTYPE html>
<html>
<head>
<script>
$(document).on('change', '#names_id', function(){
var custId = $(this).val();
return custId;
});
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3">
<div class="panel panel-primary">
<div class="panle-heading">Panel Primary</div>
<div class="panel-body">
<%= simple_form_for @ruby, url:{action: 'create'}, html: {class: 'form'} do |f| %>
<%= f.select :names_id, options_for_select(Names.distinctnames), {}, {:multiple => true} %>
<%= f.select :city_name, options_for_select(Names.where(names_id: custId).pluck(:city_name)), {}, {:multiple => true} %>
<% end %>
</div>
</div>
</div>
</div>
<div>
</body>
</html>
Here, at the time of loading the the view I get undefined local variable or method `custId' for #<#
How to load the all other dropdowns empty and then bind the value from the selected dropdown value to the second one.