javascript - how do i pass js variable in the where clause rails 4 -
<div class="field"> <%= f.label :section_id %> <%= f.select :section_id,options_from_collection_for_select(section.all, :id, :name )%> </div> <div class="field"> <%= f.label :sub_section_id %> <div id="sub_section_div"></div> </div> <div class="actions"> <%= f.submit %> </div> <script type="text/javascript"> $(document).ready(function(){ $("select#sub_section_section_id").change(function(){ var id_value_string = $(this).val(); **$('#sub_section_div').html('<%= j f.select :name,options_from_collection_for_select(subsection.where(:section_id=>1), :id, :name )%>');** }); }); </script> <% end %>
i have 2 dropdowns section
, subsection
in view page.when select section want populate respective sub-section. problem want pass id_value_string
section_id
in clause.please me out.thanks.
it wont work this, can't pass js ruby after page has loaded. think you'd better off using ajax:
$(document).ready(function(){ $("select#sub_section_section_id").change(function(){ var id_value_string = $(this).val(); $.ajax({ url: "<%= sub_sections_path %>", data: { section_id: id_value_string } }); });
you need create new controller/action this, in associated .js view file can line had passing variable sent via ajax:
$('#sub_section_div').html('<%= j f.select :name,options_from_collection_for_select(subsection.where(:section_id=>params[:section_id), :id, :name )%>');
Comments
Post a Comment