0

I am upgrading an app from Rails 4.2 to Rails 5.2.

In this app, there is a situation:

  1. jQuery $.ajax() submits to requests#create with type: 'POST', dataType: 'script'.
  2. The controller redirects to another action, maintaining format: :js.
  3. The second action receives the call as JS and renders a .js.erb file.
  4. In Rails 4, this .js.erb would run as it's supposed to.
  5. 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

Related Questions

Is it possible to disable Turbolinks for specific jQuery Ajax calls to prevent the page from refreshing and scrolling?

Community
  • 1
  • 1
sscirrus
  • 55,407
  • 41
  • 135
  • 228

0 Answers0