仅当 .js 格式时才在控制器中查询(如果是 .html 则不查询)

发布于 2024-11-17 00:57:18 字数 165 浏览 1 评论 0原文

采用标准控制器操作重定向代码:

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

如果格式为 .js,是否有办法仅执行几个附加查询?

Take the standard controller action redirect code:

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

Is there a way to only perform a couple of additional queries if the format is .js?

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

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

发布评论

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

评论(2

何以畏孤独 2024-11-24 00:57:18

当然,只需将它们添加到 format.js 响应程序块中

respond_to do |format|
  format.html { redirect_to companies_url }
  format.js {
    couple(:of)
    additional_queries
  }
end

Sure, just add them in the format.js responder block

respond_to do |format|
  format.html { redirect_to companies_url }
  format.js {
    couple(:of)
    additional_queries
  }
end
滥情空心 2024-11-24 00:57:18

如果您只想处理操作中的 .js 格式,您可以像这样指定 respond_to :js

class SomeController < ApplicationController
  respond_to :js
  def some_action
  ...
  end
end

..并且此操作将仅响应 .js 格式。或者您可以在操作中使用 request.xhr? 来验证传入格式。

if you want to handle only .js format from action you can specify respond_to :js like this:

class SomeController < ApplicationController
  respond_to :js
  def some_action
  ...
  end
end

..and this action will respond only to .js format. Or you can use request.xhr? in your action to verify incoming format.

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