Jquery Rails ajax调用未正确呈现my format.js文件
我试图模仿这一点,使用ajax创建动态下拉列表 每次我更改第一个下拉列表时,它都会调用ajax函数并进入正确的js.erb文件,如下面的日志条目所示,但它不会在控制台中记录任何内容 日志条目:Jquery Rails ajax调用未正确呈现my format.js文件,jquery,ruby-on-rails,ruby,ruby-on-rails-3,Jquery,Ruby On Rails,Ruby,Ruby On Rails 3,我试图模仿这一点,使用ajax创建动态下拉列表 每次我更改第一个下拉列表时,它都会调用ajax函数并进入正确的js.erb文件,如下面的日志条目所示,但它不会在控制台中记录任何内容 日志条目: "available slots = 20" Started POST "/arrangements/timeslots_by_location" for 127.0.0.1 at 2013-03-11 17:59:25 -0500 Processing by ArrangementsControll
"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)
/视图/安排/时间段按_location.js.erb
console.log('testing');
$("#arrangement_timeslot_id").html('<option value="2">TEST</option>');
视图/安排/_form.html.erb
<%= collection_select(:arrangement, :location_id, Location.all, :id, :name) %>
这是怎么回事?我想出来了。我还没有发布我的整个时间段 这段代码就在那里:
console.log('test = <%= options_for_select(@available_timeslots.map {|sc| [sc.timeslot, sc.id]}).gsub(/n/, '').html_safe %>');
console.log('test=');
而
.gsub(/n/,“”)
应该是.gsub(/\n/,“”)
,所以它导致了,你能给我们看一下通过_location.JS对时隙进行POST调用的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();
});
console.log('test = <%= options_for_select(@available_timeslots.map {|sc| [sc.timeslot, sc.id]}).gsub(/n/, '').html_safe %>');