I'm in the process of migrating a legacy Rails 5.0 app to (hopefully) 7.x. I got to 5.2.x without too much trouble. I'm currently trying to upgrade to 6.0 and I have a problem with controller actions that are set up to render either js or html depending on the type of request. For example, in a standard 'new' action of the general form:
def new
@post = Post.new
respond_to do |format|
format.js
format.html
end
end
if a jQuery ajax request is sent with dataType: 'script', the server renders the views/posts/new.html.erb template instead of views/posts/new.js.coffee. The console confirms that the request is received as a js request but that html is being rendered, e.g.
Started GET "/posts/new" for 127.0.0.1
Processing by PostsController#new as JS
Rendering posts/new.html.erb
Rendered posts/new.html.erb
However, if I remove new.html.erb from the view directory, then the server renders the js template as required.
Started GET "/posts/new" for 127.0.0.1
Processing by PostsController#new as JS
Rendering posts/new.js.coffee
Rendered posts/new.js.coffee
I'm seeing the same behavior with a number of different controllers/actions.
Any suggestions on what I'm missing? FWIW, I am not using webpacker, just regular sprockets. And ruby 2.6.6. The only thing I've changed from 5.2.x related to javascript is to add an assets/config/manifest.js file with the following:
//= link_tree ../images
//= link_tree ../javascripts .js
//= link_directory ../stylesheets .css
Edit: Also FWIW, I added a block with a test statement to the #js and #html methods -
respond_to do |format|
format.js {puts "calling js"}
format.html {puts "calling html"}
end
and confirmed that the #js method is the one that's being called, even though it is rendering the html template.
new.js.erbwork instead of .coffee?new.js.erbdoes work! So apparently Rails 6 does not handle coffeescript automatically. I can't see anything in the release notes for 5.0, 5.1, 5.2 or 6.0 to indicate that this was changed. What is the appropriate way to get coffeescript compiled under Rails 6? I tried downgraded sprockets from 4.2.1 to 3.7.2, the last version I had before upgrading to Rails 6, but that hasn't solved the problem.