I am upgrading an app from Rails 4.2 to Rails 5.2.
In this app, there is a situation:
- jQuery
$.ajax()
submits to requests#create withtype: 'POST', dataType: 'script'
. - The controller redirects to another action, maintaining
format: :js
. - The second action receives the call as JS and renders a
.js.erb
file. - In Rails 4, this
.js.erb
would run as it's supposed to. - In Rails 5, my webpage disappears and the
.js.erb
file is shown in the browser as-is.
My investigations (example) lead me to believe that this is due to Turbolinks, which likes to replace the page with whatever the response is from the server.
Update: removing turbolinks fixes the problem. I'd like to be able to fix it without disabling turbolinks.
How can I get the .js.erb
to be run properly rather than simply rendered?
Environment
ruby 2.6.3
rails 5.2.3
jquery-rails 4.3.3
turbolinks 5.2.0
Code
There's lots of code overall. Here are the parts that seem to make the most sense to me: the AJAX call to the server and the server's response.
# jQuery:
$.ajax({
type: "POST",
dataType: "script",
url: "/requests?c=" + c_id,
success: function(data){
...
}
})
# second controller action
...
respond_to do |format|
format.js
end