I'm having trouble with using coffee on an ajax request on a change event for a select input.
My actual script works on the events:
$ ->
$(document).ready ->
$(document).on 'change', '#empresa_id', (evt) ->
$.ajax '/empleados/find_branches',
type: 'POST'
dataType: 'json'
data: {
id: $("#empresa_id option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log(jqXHR)
console.log(textStatus)
console.log(errorThrown)
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
for key, value of data['data']
$('#sucursales_id').append($('<option>').text(value.nombre).attr('value', value.id));
I have on application.html.erb:
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
On application.js:
//= require rails-ujs
//= require jquery3
//= require activestorage
//= require turbolinks
//= require_tree .
//= require popper
//= require bootstrap
But i still get:
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)
I know that //= require rails-ujs actually add the token on the header of the request but on the browser doesn't show it.
I already do rake assets:precompile.
Am i missing something?
: