我通过 DataTable 显示数据并检测用户何时选择了一行。然后我想加载该行的编辑表单。
var oTable;
$(document).ready(function() {
/* Init the table */
TableTools.DEFAULTS.aButtons = [];
oTable = $("#neighbours").dataTable({
sPaginationType: "full_numbers",
bJQueryUI: true,
"bProcessing": true,
"bServerSide": true,
iDisplayLength: 25,
sAjaxSource: $('#neighbours').data('source'),
"aoColumns": [
null,
null,
null,
null,
null
],
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sRowSelect": "single",
"fnRowSelected": function ( node ) {
var oTT = TableTools.fnGetInstance('neighbours');
var aData = oTT.fnGetSelectedData();
// alert (aData[0][4]);
n = aData[0][4];
$.get('edit/' + n);
}
},
});
我的相关控制器代码:
def edit
logger.debug "Edit in Controller for Neighbour ID #{params[:id]}"
@neighbour = Neighbour.find(params[:id])
@localities = Locality.find(:all)
@highways = Highway.find(:all)
end
我可以从日志中看到正确的 id 已传递给控制器,并且 WebBrick 报告该视图已按此摘录所示呈现。
Started GET "/neighbours/edit/39" for 127.0.0.1 at 2012-08-08 18:10:20 +1000
Processing by NeighboursController#edit as */*
Parameters: {"id"=>"39"}
Edit in Controller for Neighbour ID 39
Rendered neighbours/_form.html.erb (4341.0ms)
Rendered neighbours/edit.html.erb within layouts/neighbours (4410.0ms)
Completed 200 OK in 5564ms (Views: 4479.6ms | ActiveRecord: 28.0ms)
该视图永远不会显示在浏览器中。但是,如果我在浏览器地址栏中输入 localhost:3000/neighbours/edit/39 ,或者在表单标题中放置一个链接,则视图呈现正常。我可以在日志中看到的唯一区别是以下行:
Processing by NeighboursController#edit as */*
通过浏览器或链接直接加载,它显示为:
Processing by NeighboursController#edit as HTML
由于控制器在任何一种情况下都能正确获取 Id,因此我不确定出了什么问题。为什么通过 JavaScript 传递 id 很重要?顺便说一句,我对不同的数据集进行了相同的尝试,但效果相同。