Rails 7 升级后的 ActionController::UnknownFormat
我有一个计算器,用户可以输入值,点击提交,并且无需刷新或重新渲染,计算出的值将显示在页面上。一切运行良好,直到我从 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:
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 (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Rails 7 中,Turbo 处理这种行为。
如果您想规避此默认设置,可以
将给定链接或表单添加为属性。
或者您可以将同一行添加到全局 javascript 文件中,例如 application.js。
请参阅此处的 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
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.
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