Rails action_controller 控制器处理 ( #show as */* )

发布于 2024-12-06 14:37:23 字数 843 浏览 0 评论 0原文

我正在向我的一个控制器#action发送一个ajax请求,但我的开发日志显示,

Processing by FormsController#show as */*

这应该是FormsController#show as JS,因为它是一个ajax请求,

因此它渲染了它在我的响应块中第一个找到的格式在控制器中

例如:-

respond_to do |format|
  format.html{ }
  format.js { }
end

如果我向控制器发送 ajax 请求,它不会按预期工作,因为我的控制器呈现 html 响应,而它应该处理 JS 请求。

但如果 respond_to 块是这样的,

respond_to do |format|
  format.js { }
  format.html{ }
end

它就会按预期工作。

我认为

Processing by FormsController#show as */* 

是响应它在respond_to 块中首先找到的任何格式。

但我担心的是为什么显示的是我的开发日志,

Processing by FormsController#show as */*

而不是

Processing by FormsController#show as JS

我发送 ajax 请求的时间。我是否做错了什么或遗漏了一个小而重要的部分?

I am sending an ajax request to one of my controller#action but my development log shows

Processing by FormsController#show as */*

while this should have been FormsController#show as JS as its an ajax request

Due to this the its rendering the format that it finds first in my responds to block in the controller

Eg:-

respond_to do |format|
  format.html{ }
  format.js { }
end

if I send ajax request to my controller it doesn't work as expected as my controller renders html response while it should process JS request.

But if the respond_to block is in this way

respond_to do |format|
  format.js { }
  format.html{ }
end

It work as expected.

I think

Processing by FormsController#show as */* 

is responding to which ever format it finds first in respond_to block.

But my concern is why my development log is showing

Processing by FormsController#show as */*

instead of

Processing by FormsController#show as JS

when I have sent an ajax request. Am I doing something wrong or missing a small but important piece?

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

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

发布评论

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

评论(1

梦过后 2024-12-13 14:37:23

更新您的 Rails.js 文件。

我假设您正在使用 jQuery-Rails。不久前,Rails AJAX 请求不会自动设置请求的内容类型。此问题已得到解决,因此如果您进行更新,则不应将 */* 作为 HTTP 标头中最高优先级的内容类型。

Update your Rails.js file.

I'm assuming you're using jQuery-Rails. Some time ago, Rails AJAX requests didn't automatically set the requested content-type. That has since been addressed, so if you update you shouldn't have */* as the highest-prioritity content-type in your HTTP headers.

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