Closed epipheus closed 4 years ago
full error:
ActionController::UnknownFormat - OrganizationsController#show is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
When I tried verifying this it was via browser so not a POST. Only thing keeping me from closing this is that I'm still not out of the woods. The POST is failing for CSRF reasons. Any way to pass CSRF token in the post?
OK. Figured it out, closing this.
Actually, the title is that the advance sample doesn't work. And that's true.
The ajax portion of that sample should be replaced with the following snippet:
"ajax": {
"url": "/blahblah/datatable.json", // or data-source
"type": 'POST',
"beforeSend": function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', token)
}
},
and perhaps add a few more options
jQuery(document).ready(function() {
var table = $('#blahblah-datatable');
var token = $('meta[name=csrf-token]').attr('content');
table.DataTable({
"sDom": "<t><'row'<p i>>",
"processing": true,
"serverSide": true,
"ajax": {
"url": "/blahblah/datatable.json", // or data-source
"type": 'POST',
"beforeSend": function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', token)
}
},
"pagingType": "full_numbers",
"destroy": true,
"scrollCollapse": true,
"processing": true,
"serverSide": true,
"oLanguage": {
"sLengthMenu": "_MENU_ ",
"sInfo": "Showing <b>_START_ to _END_</b> of _TOTAL_ entries"
},
"columns": [
{"data": "blah"},,
{"data": "blah"},,
{"data": "blah"},
{"data": "blah"}
],
"iDisplayLength": 50
});
I think cleaner code is to use headers option
"ajax": {
"url": "/blahblah/datatable.json", // or data-source
"type": 'POST',
"headers": { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') },
},
based on https://datatables.net/forums/discussion/31574/adding-a-http-header-to-the-ajax-request
Weird... It should works out of the box. It seems you don't use UJS : https://guides.rubyonrails.org/security.html#csrf-countermeasures
By default, Rails includes an unobtrusive scripting adapter, which adds a header called X-CSRF-Token with the security token on every non-GET Ajax call. Without this header, non-GET Ajax requests won't be accepted by Rails.
Rails version: 5.2.3 Ruby version:2.0.4
using concerns with post like the sample says ends up with a clash with #show.
routes.rb
These are my routes:
Navigating to the route, /organizations/datatables.json produces this error:
OrganizationsController#show is missing a template for this request format and variant.
It thinks/organizations/datatable.json
is a match for/organizations/:id/show
despite having a clear route for/organizations/datatable(.:format)
Here's my controller: