javascript - Rails Ajax dropdown menu - no response -
so i've solved problems little program i'm writing:
the 1 left, i'm beating head against, goes this.
it's standard 2 dropdown menus, select item on 1 populate other.
in view:
<%= select_tag( :contractor_id, options_from_collection_for_select(contractor.all, "id", "appointment_record"), :'data-remote' => 'true', :'data-url' => url_for(:controller => 'contractors', :action => 'getdata'), :'data-type' => 'json') %> <%= select_tag( :company_id, # name of selectbox ) %>
in controller:
def getdata @data_from_select1 = params[:contractor_id] @data_for_select2 = company.where(:id => @data_from_select1).all # render array in json containing arrays like: # [[:id1, :name1], [:id2, :name2]] render :json => @data_for_select2.map{|c| [c.id, c.name]} end
in application.js:
$(document).ready(function() { $('#contractor_id').live('ajax:success', function(evt, data, status, xhr) { var selectbox2 = $('#company_id'); selectbox2.empty(); $.each(data, function(index, value) { // append option var opt = $('<option/>'); // value array: [:id, :name] opt.attr('value', value[0]); // set text opt.text(value[1]); // append select opt.appendto(selectbox2); }); }); });
in routes:
'getdata' => 'contractors#getdata'
and don't @ populating company.id. not ever!
contractor.id populates fine, names fill out there, nothing returned company.id
i'm huge noobie, i'm sure i'm missing here. what??
have tried adding options in different way? example
var selectbox2 = $('#company_id'); var option = document.createelement("option"); //adds value option.value = value[0]; // adds in text option.innerhtml = value[1]; // appends option select box selectbox2[0].appendchild(option);
you can console.log(value); , see if getting populated response or not.
Comments
Post a Comment