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:
When I add format.html
to the respond_to
block, I get this error:
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 (ie 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 ...
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.