Rails 7 升级后的 ActionController::UnknownFormat

发布于 2025-01-11 22:13:27 字数 1455 浏览 2 评论 0原文

我有一个计算器,用户可以输入值,点击提交,并且无需刷新或重新渲染,计算出的值将显示在页面上。一切运行良好,直到我从 Rails 6.1.4.6 升级到 7.0.2.2。现在我收到以下错误:

在此处输入图像描述

当我将 format.html 添加到 respond_to 块时,出现以下错误:

在此处输入图像描述

当我将 format.html { render :new } 添加到 respond_to 块时,页面重新渲染new.html.erb 文件,但没有计算值(即 create.js.erb 文件未运行)。

任何帮助将不胜感激。

以下是在 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 ...
}

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 ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2025-01-18 22:13:27

在 Rails 7 中,Turbo 处理这种行为。

如果您想规避此默认设置,可以

Turbo.session.drive = false

将给定链接或表单添加为属性。

或者您可以将同一行添加到全局 javascript 文件中,例如 application.js。

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

请参阅此处的 Turbo 指南。

或者,如果该项目中根本不会使用 Turbo 和 Hotwire,并且您不需要额外的 gem 依赖项,则可以完全删除 Hotwire gem。这可以通过遵循此 Stack Overflow 答案来完成

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文