2013-03-11 2 views
0

Я пытаюсь имитировать этот http://samuelmullen.com/2011/02/dynamic-dropdowns-with-rails-jquery-and-ajax/, чтобы сделать динамические выпадающие списки с помощью ajax.Rails вызов ajax не возвращает мой файл format.js правильно

Каждый раз, когда я меняю свой первый выпадающий список, он вызывает мою функцию ajax и попадает в правильный файл js.erb, как видно из записи журнала ниже, но он ничего не записывает в консоли.

запись

Log:

"available slots = 20" 


Started POST "/arrangements/timeslots_by_location" for 127.0.0.1 at 2013-03-11 17:59:25 -0500 
Processing by ArrangementsController#timeslots_by_location as JS 
    Parameters: {"id"=>"3"} 
    User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 
    (2.8ms) SELECT COUNT(*) FROM `timeslots` WHERE (location_id = '3' AND arrangement_id is null) 
    Timeslot Load (0.4ms) SELECT `timeslots`.* FROM `timeslots` WHERE (location_id = '3' AND arrangement_id is null) ORDER BY timeslot ASC 
    Rendered arrangements/timeslots_by_location.js.erb (6.3ms) 
Completed 200 OK in 19ms (Views: 11.3ms | ActiveRecord: 3.6ms) 
"available slots = 20" 


Started POST "/arrangements/timeslots_by_location" for 127.0.0.1 at 2013-03-11 17:59:25 -0500 
Processing by ArrangementsController#timeslots_by_location as JS 
    Parameters: {"id"=>"3"} 
    User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 
    (0.4ms) SELECT COUNT(*) FROM `timeslots` WHERE (location_id = '3' AND arrangement_id is null) 
    Timeslot Load (0.5ms) SELECT `timeslots`.* FROM `timeslots` WHERE (location_id = '3' AND arrangement_id is null) ORDER BY timeslot ASC 
    Rendered arrangements/timeslots_by_location.js.erb (9.4ms) 
Completed 200 OK in 22ms (Views: 16.8ms | ActiveRecord: 1.3ms) 

/views/arrangements/timeslots_by_location.js.erb

console.log('testing'); 
$("#arrangement_timeslot_id").html('<option value="2">TEST</option>'); 

В поджигатель, если я расширить пост вызов из консоли, я вкладку ответа и Я вижу свой код javascript, но на самом деле он не входит в консоль или не добавляет мой вариант в раскрывающийся список.

/controllers/arrangements_controller.rb

# Gets the available timeslots based on the location selected 
    def timeslots_by_location 
    if params[:id].present? 
     @available_timeslots = Timeslot.where('location_id = ? AND arrangement_id is null', params[:id]).order('timeslot ASC') 
    else 
     @available_timeslots = [] 
    end 
    p "available slots = #{@available_timeslots.size}" 

    respond_to do |format| 
     format.js 
    end 
    end 

просмотров/мероприятия/_form.html.erb

<%= collection_select(:arrangement, :location_id, Location.all, :id, :name) %> 

активы/JavaScripts/arrangements.js

// Setup ajax calls to hit the format.js respond_to in my controller 
jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) { 
     xhr.setRequestHeader("Accept", "text/javascript"); 
    } 
});  

// function that gets called when the location dropdown changes 
$.fn.subSelectWithAjax = function() { 
    var that = this; 

    this.change(function() { 
     $.post('/arrangements/timeslots_by_location', {id: that.val()}, null, "script"); 
    }); 
} 

// Call the subSelectWithAjax function when the location dropdown changes 
$(document).ready(function() { 
    $("#arrangement_location_id").subSelectWithAjax(); 
}); 

Что с это?

+0

Можете ли вы показать нам JS, который делает запрос POST на timeslots_by_location.js? – Steve

+0

Обновлено с этой информацией. – Catfish

ответ

0

Я понял. Я не опубликовал весь файл timeslots_by_locations.js.erb.

Этот фрагмент был там:

console.log('test = <%= options_for_select(@available_timeslots.map {|sc| [sc.timeslot, sc.id]}).gsub(/n/, '').html_safe %>'); 

и .gsub(/n/, '') должны были .gsub(/\n/, '') так причинял <option .. теги быть <optio ...

Смежные вопросы