I display data via DataTable and detect when the user has selected a row. I want to then load the edit form for that row.
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);
}
},
});
My relevant controller code:
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
I can see from the log that the correct id has passed to the controller and WebBrick reports that the view has been rendered as this extract shows.
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)
The view never shows in the browser. However if I enter localhost:3000/neighbours/edit/39 in the browser address bar, or place a link in the form header the view is rendered OK. The only difference I can see in the log is the line:
Processing by NeighboursController#edit as */*
With the direct load via the browser or the link it shows as:
Processing by NeighboursController#edit as HTML
Since the controller just gets the Id correctly in either case I am not sure what is wrong. Why does it matter if the id is passed by JavaScript? By the way, I have tried the same with different data sets, but with the same effect.
Your controller for edit code might reveal the problem, the default code does only return data for HTML and JSon, so it probably returns nothing for this call. Consider always returning HTML by removing the case:
this is the default code:
and change that to force html to be returned by just calling render instead of this case statement.
BTW: the
*/*stand for :all