'ActionController::UnknownFormat After Rails 7 Upgrade

I have a calculator which users would enter in values, hit submit and without refreshing or re-rendering, the calculated values would display on the page. All worked well until I upgraded from Rails 6.1.4.6 to 7.0.2.2. Now I receive this error below:

enter image description here

When I add format.html to the respond_to block, I get this error:

enter image description here

When I add format.html { render :new } to the respond_to block, the page re-renders the new.html.erb file but without calculated values (i.e create.js.erb file is not running).

Any help would be appreciated.

Here are the files that ran perfectly in Rails 6.

calculator_controller.rb

def create
  @calculation = calculate_one_option

  respond_to do |format|
    format.js
  end
end

def new; end

new.html.erb

<%= form_tag calculate_path, method: :post, remote: true do %>
  ... form stuff ...
  <%= submit_tag "Calculate" %>
<% end %>

create.js.erb

var formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });

if ("<%= @calculation.error %>".length > 0 ) {
  alert("<%= @calculation.error %>");
  ... js stuff ...
} else {
  ... more js stuff ...
}



Solution 1:[1]

In Rails 7, Turbo handles this behavior.

If you want to circumvent this default, you can add

Turbo.session.drive = false

on a given link or form as an attribute.

Or you can add the same line to a global javascript file, such as the application.js.

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

See the Turbo guide here.

Alternatively, if Turbo and Hotwire will not be used at all in this project and you do not want additional gem dependencies, you could remove the Hotwire gem completely. This can be done by following this Stack Overflow answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joe Thor